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,

Remove spaces from a string

[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]:~$ 

Removing multiple spaces from a string

One more example, altogether, where the spaces between the word and numbers were deleted.

[email protected] ~> myvar="codetryout 1 2 3"
[email protected] ~>
[email protected] ~> echo ${myvar// }
codetryout123