Replace dollar sign with sed

I am trying to replace all dollar signs in a string with sed. However, not only the dollar sign is replaced, but the entire subsequent line.

$ echo "abc $def ghi" | sed 's/$//g'
$ abc ghi

If at least one number matches the dollar sign, then only the part to the first non-number is replaced:

$ echo "abc $123def ghi" | sed 's/$//g'
$ abc def ghi

What's happening?

+4
source share
3 answers
echo 'abc $def ghi' | sed 's/\$//g'

Echo uses a single quote if it does not mean that there is a def variable and its substitution, and if you do not have a def variable, it is empty. In sed, you need to avoid the dollar sign, because otherwise it means "snap to end of line".

+9
source

"$" , . :

v1='abc $def ghi'
v2='abc $123def ghi'
echo ${v1/$/}
echo ${v2/$/}

: ${parameter/pattern/string}

Shell, : https://www.gnu.org/software/bash/manual/html_node/Shell-Parameter-Expansion.html#Shell-Parameter-Expansion

-1

trshould be used for this task, not sed.

Use it with single quotes in echoto prevent options from expanding.

echo 'abc $123def ghi' | tr -d "$"

-1
source

Source: https://habr.com/ru/post/1683225/


All Articles