linux - 如何转义密码中的双引号和感叹号?

标签 linux bash shell sh

我有以下代码:

curl -s --insecure -H "Content-Type: application/json" -X POST -d "{\"username\":\"$1\",\"password\":\"$2\"}" http://apiurl

在上面的 curl 命令中,我想转义密码中的 "!。现在密码只是作为 $2

我已经如下修改了 curl 命令,但它似乎不起作用

curl -s --insecure -H "Content-Type: application/json" -X POST -d "{\"username\":\"$1\",\"password\":\"$2\"}" http://apiurl

最佳答案

不要尝试手动生成 JSON;使用像 jq 这样的程序,它知道如何正确地转义,为你生成它。

json=$(jq -n --arg u "$1" --arg p "$2" '{username: $u, password: $p}')
curl -s --insecure -H "Content-Type: application/json" -X POST -d "$json" http://apiurl

关于linux - 如何转义密码中的双引号和感叹号?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43954701/

相关文章:

bash - 如何检查用户是否输入了单个字母?

linux - optee trust os 在树莓派 3 上的实现

bash - 在 Yosemite 中设置环境变量

debugging - 意外的运算符(operator) [ : git: in bourne shell script 'if' conditional statement

linux - 从命令列表中调用 shell 命令,直到所有命令都已完成

bash - 如何在 OSX 终端中输入多行?

linux - 应用程序如何在运行时解析为不同版本的共享库?

linux - Centos 启动发送串口信息

linux - MPlayer – 在 mplayer.conf 中更改 ‘ao=’ 编解码器顺序似乎不适用于每个用户配置?

bash - 错误 : the option `Z` is only accepted on the nightly compiler