【问题标题】:Passed command line argument in double quotes to curl将双引号中的命令行参数传递给 curl
【发布时间】:2018-01-27 06:04:09
【问题描述】:

我的脚本正在使用命令行参数,它需要用双引号将其传递给 curl 命令。这是我迄今为止尝试过的简化版本:

json=$1;
echo $json;

curl -X POST -d '{"asin":\"$json\", "template":"bolt","version":"1d"}' -H "Content-Type: application/json" http://someURL

但它不起作用。请帮忙

【问题讨论】:

标签: shell curl


【解决方案1】:

单引号字符串中的$-变量不会被扩展。 -d 参数需要用双引号括起来,或者至少 $json 部分需要是:

curl -X POST -d '{"asin":"'"$json"'", "template":"bolt","version":"1d"}' -H "Content-Type: application/json" http://someURL

' - 终止单引号字符串,然后是"$json",然后' 开始一个相邻的单引号字符串。

"$json" 变量不应扩展为包含非转义双引号的字符串,否则生成的 json 将被破坏。

【讨论】:

    猜你喜欢
    • 2013-02-02
    • 2011-04-26
    • 2016-06-25
    • 2018-11-17
    • 1970-01-01
    • 2020-10-03
    • 2013-04-19
    • 2020-03-19
    相关资源
    最近更新 更多