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. Is there a solution?
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'"