I have this very reduced example of a bash command, where I want the $ sign escaped. So the command :
su -m user -c "echo $test"
should print out:
$test
a simple $test does not work unfortunately. I tried lots of other stuff but still couldn’t find a solution. Any suggestions ?
Advertisement
Answer
Put it in single quotes rather than double quotes.
su -m user -c 'echo $test='
The single quotes keep the variable from being expanded by the original shell. The backslash then escapes the dollar sign in the shell run by su
.
See Difference between single and double quotes in Bash
In answer to the comment, you can switch to double quoting to get single quotes into the string.
su -m user -c 'echo $test='"'1'"