在 bash 脚本中正确转义 Json 命令行选项
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14037514/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Properly escaping Json command line option in bash script
提问by Vivek Goel
I have utility which accept JSON as argument. How to escape json correctly to pass to the utility? Example:
我有接受 JSON 作为参数的实用程序。如何正确转义 json 以传递给实用程序?例子:
ip="127.0.0.1"
action='{\"server_ip\":\"$ip\",\"action\":\"stop\"}'
./fix-utility -e $action
But JSON is not getting escaped correctly.
但是 JSON 没有被正确转义。
回答by tripleee
If you want interpolation of e.g. $ipinside the string, you need to use double quotes. Double quotes in the value need backslash escaping.
如果要$ip在字符串内插入 eg ,则需要使用双引号。值中的双引号需要反斜杠转义。
ip="127.0.0.1"
action="{\"server_ip\":\"$ip\",\"action\":\"stop\"}"
./fix-utility -e "$action"
Actually, I would advise against storing the action in a variable, unless your example omits something crucial which makes it necessary.
实际上,我建议不要将操作存储在变量中,除非您的示例省略了一些使其成为必要的关键内容。
回答by F. Hauri
Double quotes and single quotes is not considered same by bash interpreter: While double-quote let bash expand contained variables, single quote don't. Try simply:
双引号和单引号不被 bash 解释器认为是相同的:虽然双引号让 bash 扩展包含的变量,但单引号不会。简单地尝试:
echo "$BASH_VERSION"
4.1.5(1)-release
echo '$BASH_VERSION'
$BASH_VERSION
At all, there is a nice reusable bash way:
总之,有一种很好的可重用 bash 方式:
declare -A jsonVar
toJsonString() {
local string='{'
for var in ${!jsonVar[*]} ;do
string+="\"$var\":\"${jsonVar[$var]}\","
done
echo ${string%,}"}"
}
jsonVar[action]=stop
jsonVar[server_ip]=127.0.1.2
toJsonString
{"action":"stop","server_ip":"127.0.1.2"}
And finally:
最后:
./fix-utility -e "$(toJsonString)"
This could be improved if specials chars like "double-quotes have to be part of some strings.
如果像"双引号这样的特殊字符必须成为某些字符串的一部分,则可以改进这一点。
回答by Thomas
Updating this old question with newer tools. This solution relies on python's jqsince it's a great tool for manipulating JSON files and strings.
用较新的工具更新这个老问题。该解决方案依赖于 python,jq因为它是处理 JSON 文件和字符串的绝佳工具。
Using the native bash string concatenation, one can do without having to escape anything manually and rely on jqto perform all the escaping.
使用本机 bash 字符串连接,您无需手动转义任何内容并依赖于jq执行所有转义。
# let's say I have need to include a variable within a string containing characters to escape
> my_string='a'"b"'c'
> echo -e "$my_string"
abc
> my_string='!(){'"$variable"'}[]'
> echo -e "$my_string"
!(){my_value_here}[]
# in effect, I'm concatenating these 3 strings together:
'!(){'
"$variable"
'}[]'
The single quotes prevent interpretation by bash, the double quote allow interpretation of the variable by bash.
单引号阻止 bash 解释,双引号允许 bash 解释变量。
Now, to escape, I would advise using python's jqprogram (pip install jq).
现在,为了逃避,我建议使用 python 的jq程序 ( pip install jq)。
> ip="127.0.0.1"
> action='{"action":"stop","server_ip":"'"$ip"'"}'
> echo -e "${action}" | jq -cM '. | @text '
"'{\"action\":\"stop\",\"server_ip\":\"127.0.0.1\"}'"
For context, here are the versions of bash and jq that I'm using:
对于上下文,这里是我正在使用的 bash 和 jq 版本:
> bash --version
GNU bash, version 3.2.57(1)-release (x86_64-apple-darwin18)
Copyright (C) 2007 Free Software Foundation, Inc.
> jq --version
jq-1.6

