bashescapingdollar-sign

How to echo a variable containing an unescaped dollar sign in Bash


If I have a variable containing an unescaped dollar sign, is there a way I can 'echo' the entire contents of the variable?

For example, something calls a script:

./script.sh "test1$test2"

and then if I want to use the parameter, it gets "truncated" like so:

echo ${1} test1

Of course, single-quoting the variable name doesn't help. How can I quote it so that I can at least escape the dollar sign myself once the script receives the parameter?


Solution

  • The variable is replaced before the script is run.

    ./script.sh 'test1$test2'