I my bash script, I have a string variable with a $ sign it, and the it isn't escaped. It looks like this:
x="hello $world"
Obviously, when I echo "$x", the output is hello, since $world is being interpreted as a variable. My goal is to modify the string to be hello \$world. I tried several techniques, listed below. None of them seem to be working:
y="$(echo "$x" | sed 's/\$/z/g')" (outputs hello)
y="$(echo "$x" | sed 's/$/z/g')" (outputs hello z, even though I didn't escape \ in sed)
Even tried Bash's native string replacement through:
y=${x//\$/z} (outputs hello)
I realize that I could easily do any of these if the string weren't stored in a variable, but the way my script works, this string will be stored in a variable first, so I need to figure out how to add the \ after that. I don't care if I create a new copy of the string or edit the same string.
sedcommand, the$(quoted from bash) matches end-of-line, as sed works with basic regular expressions.$worldis gone and you can't get it back, as shown in Toby's answer.sedcommand with a single-quoted'$x'like this:y="$(echo '$x' | sed 's/\$/z/g')". This version sets$ytozx.jqto be specific) that returns a string that has several unescaped$signs in it.strcontaining an unescaped$, likehello $world(and it shows up exactly like that when you issuedeclare -p str), doingecho "$str"will not try to expand the variable name. Can you show your actual assignment?