Simple Bash Question

Home » CentOS » Simple Bash Question
CentOS 4 Comments

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.

Jerry

4 thoughts on - Simple Bash Question

  • Hi Jerry,

    The problem is that bu the time you’re in your script it’s already too late.

    myscript “$plusmore”

    basically says take the contents of the environment plusmore and pass that to the script.

    if you call

    myscript ‘$plusmore’

    you will get what you want.

  • 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

  • Sounds like you’re pretty constrained if you can’t escape $plusmore so alternatives may not be possible either but, if possible, put the contents represented by $plusmore in a file and {read,redirect the output from} the file in myscript. Another option might be to put the contents represented by $plusmore in an environment variable and access that from myscript. The only other option I can think of is to try a different scripting language (awk, perl, tcl, python, you-name-it) to try and get around the issue.

    Join us at the 2018 Momentum User Conference!
    Register here Leroy Tennison Network Information/Cyber Security Specialist E: leroy@datavoiceint.com
    2220 Bush Dr McKinney, Texas
    75070
    http://www.datavoiceint.com TThis message has been sent on behalf of a company that is part of the Harris Operating Group of Constellation Software Inc. These companies are listed here
    . If you prefer not to be contacted by Harris Operating Group please notify us
    . This message is intended exclusively for the individual or entity to which it is addressed. This communication may contain information that is proprietary, privileged or confidential or otherwise legally exempt from disclosure. If you are not the named addressee, you are not authorized to read, print, retain, copy or disseminate this message or any part of it. If you have received this message in error, please notify the sender immediately by e-mail and delete all copies of the message.

  • myscript ‘$plusmore’

    should work. note the single quotes.

    If you can’t use single quotes, I don’t know how to use it,, but there is probably something more arcane that would work, were one a guru.

    Have you read thru ‘man bash’, carefully, looking for informatiion on quoting?

    there is also a document named Advanced Bash Scripting that gives lots of gory details about doing odd things in Bash that might be helpful. Google is your friend.

    Fred