linux - 如何传递带有特殊字符的参数来调用 shell 脚本

标签 linux bash shell quoting

使用以下所需参数调用 .sh(shell 脚本):-

sh home/example.sh --context_param dbUserName=username --context_param dbPassword=exam!ple##### --context_param resultDate=2017-01-13

使用参数 dbUsername 和密码调用 example.sh 但出现以下错误:-

-bash: !ple#####: event not found

我认为特殊字符会限制命令的执行。那么我必须如何传递特殊字符。任何帮助都将不胜感激。

最佳答案

换行,

dbPassword=exam!ple#####

到,

dbPassword='exam!ple#####'

避免 !(历史扩展)在 bash 中被特殊对待

来自 man bash 下的 QUOTING 子节,

When the command history expansion facilities are being used (see HISTORY EXPANSION below), the history expansion character, usually !, must be quoted to prevent history expansion.

HISTORY EXPANSION下的更多信息

History expansions are introduced by the appearance of the history expansion character, which is ! by default. Only backslash (\) and single quotes can quote the history expansion character.

此外,最好引用所有 name-value 对,以防止 shell 进行分词。

sh home/example.sh --context_param dbUserName="username" --context_param dbPassword='exam!ple#####' --context_param resultDate="2017-01-13"

关于分词,来自 man 页面,

Word Splitting

The shell scans the results of parameter expansion, command substitution, and arithmetic expansion that did not occur within double quotes for word splitting. The shell treats each character of IFS as a delimiter, and splits the results of the other expansions into words using these characters as field terminators

关于linux - 如何传递带有特殊字符的参数来调用 shell 脚本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42106102/

相关文章:

python - Python脚本与linux shell的交互

bash - 在多台机器上并行运行脚本

json - 通过 jq 为每个 JSON 项运行 bash 命令

linux - Bash:如何转换此文本以满足我的需要?

c++ - 获取 CPU/内存利用率的跨平台方式

linux - 用于查找和调整图像大小的 Shell 脚本

bash - Unix shell 之间的可移植性——我对这个问题的思考是否正确?

regex - BASH glob/regex 范围的奇怪行为

linux - 如何从ip中提取前2个数字

bash - Scala 2.12.12 的 Shell 脚本损坏了?