thetux4 thetux4 - 2 months ago 7
Bash Question

Shell script variable replacing characters

I have a variable var="abcde$$$$$$$$fff$$gg". I want to replace all $ with space ' ' but the following puts just one space

var=$( echo "$var" | tr '$' ' ')


How can i replace them all?

Answer

Works for me. You'll need to use single quotes or escape the dollar signs, otherwise they are removed from the double-quoted string:

echo 'abcde$$$$$$$$fff$$gg' | tr '$' ' '
abcde        fff  gg

echo "abcde\$\$\$\$\$\$\$\$fff\$\$gg" | tr '$' ' '
abcde        fff  gg

echo abcde\$\$\$\$\$\$\$\$fff\$\$gg | tr '$' ' '
abcde        fff  gg

echo $'abcde$$$$$$$$fff$$gg' | tr '$' ' '
abcde        fff  gg