简体   繁体   中英

Variable with $ in value shell script

I have variable - with value "RzQh$TaH6Vq5bD" but when i do export TASK_UID=$1 where $1 is argument to shell script ie RzQh$TaH6Vq5bD It ignores anything after $ it only gives me "RzQh"

Please , suggest so that it will consider value as it is.

Try with escape sequence

Replace RzQh$TaH6Vq5bD with RzQh\$TaH6Vq5bD

It will works.

You can use single quotes:

a='RzQh$TaH6Vq5bD'

or just escape the $ with a \\ like so:

a="RzQh\$TaH6Vq5bD"

Both will retain the original value without trying to process it as a variable.

The TASK_UID=$1 is most likely not the problem. The problem is how the script is called.

Without proper quoting (or escaping the $ ) when calling the script, the argument will be already expanded, meaning, the `$TaH6Vq5bD" is treated as a variable, which if not defined, results in nothing. And your script will never know about it.

The script tst.ksh

#!/bin/ksh
TASK_UID=$1
echo "$TASK_UID"

will act as follows

prompt $ tst.ksh RzQh$TaH6Vq5bD
RzQh
prompt $ tst.ksh "RzQh$TaH6Vq5bD"
RzQh
prompt $ tst.ksh 'RzQh$TaH6Vq5bD'
RzQh$TaH6Vq5bD
prompt $ tst.ksh RzQh\$TaH6Vq5bD
RzQh$TaH6Vq5bD
prompt $ argument='RzQh$TaH6Vq5bD'
prompt $ tst.ksh $argument
RzQh$TaH6Vq5bD

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM