简体   繁体   English

带有$的值的shell脚本变量

[英]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" 我有变量-值“ RzQh $ TaH6Vq5bD”,但是当我导出TASK_UID = $ 1时,其中$ 1是shell脚本的参数,即RzQh $ TaH6Vq5bD它忽略了$之后的任何内容,只给了我“ 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. TASK_UID=$1很可能不是问题。 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. 调用脚本时,如果没有正确的引号(或转义$ ),则参数将被扩展,这意味着,`$ TaH6Vq5bD“被视为变量,如果未定义,将不会产生任何结果。您的脚本将永远不会知道它。

The script tst.ksh 脚本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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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