使用以下所需参数调用 .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/