How to remove spaces from a string variable in bash?

In a bash script, spaces can be quickly removed as follows,

echo ${myvar// } # this will remove all spaces in $myvar

Let us look at a detailed example,

[email protected]:~$ myvar="  "
[email protected]:~$ echo $myvar

[email protected]:~$

Empty line. Let us make it more visible:

[email protected]:~$ echo START${myvar}END
START END
[email protected]:~$ echo START${myvar//}END
START END
[email protected]:~$ 

Now, let us replace all spaces in it

[email protected]:~$ echo START${myvar//}END
START END
[email protected]:~$ echo START${myvar// }END
STARTEND
[email protected]:~$ 

One more example, all together

[email protected]:~$ myvar="codetryout.com"
[email protected]:~$ 
[email protected]:~$ echo ${myvar// }
codetryoutinfo
[email protected]:~$