1

I need to pass a string argument to a bash script that may contain a $ character. I don't want to force a \ to be inserted into the string outside of the script.

I tried to do that within the script, but couldn't figure out how to do this.

I had a similar issue at a later point in the script where I read in a string using "read". I could only get it to work by forcing the user to enter \$, which is not going to work for my application.

Any suggestions ?

4

1 回答 1

3

如果您不想$用反斜杠转义,那么唯一的另一种选择是将参数括在单引号中。无法将“naked”传递$到您的脚本中,因为 shell 会尝试扩展它。使用单引号可防止 shell 扩展,并将保留$.

例如:

myscript.sh '$foo'
于 2012-12-14T13:27:32.847 回答