[CentOS] Simple bash question

Anand Buddhdev anandb at ripe.net
Fri Sep 28 13:51:08 UTC 2018


On 28/09/2018 15:39, Jerry Geis wrote:

> I am calling a bash script and passing in somestring that includes a "$"
> 
> myscript   "$plusmore"
> 
> I want to assign in the myscript the $1 arg to something like
> MYTEXT="$1"
> 
> when I do that I dont get what I'm expecting. if I do
> MYTEXT='$1'
> I still dont get what I'm expecting.
> 
> On the first assignment of MYTEXT I do not want the "$" to be treated as a
> shell variable. I cannot find out how to do that.
> 
> I do not have the option of escaping the call to myscipt "\$plusmore". I
> cannot do that.
> 
> What am I missing.

You MUST escape the $ in plusmore. If you don't, the calling shell will
try to expand it, and replace it with whatever is in that variable. If
it's not defined, you'll get an empty string. All this happens *before*
myscript is even called.

I'll add that escaping the $ can be done in other ways. Instead of a
backslash, you can also do:

myscript '$plusmore'

Single quotes prevent variable expansion. However, if you are simply
unable to quote $plusmore in some way, then you're stuck.

Anand



More information about the CentOS mailing list