Bash脚本:curl命令如果是原始字符串,则有效,但如果是变量,则无效

时间:2018-09-20 03:59:33

标签: bash curl flask

我在Bash上遇到以下问题:如果我将命令作为原始字符串提供,但是如果我将该字符串存储在变量中,然后尝试执行(评估)该变量,即 data参数,则运行命令有效无法识别:

#!/usr/bin/env bash

# this works
out=$(curl -s http://localhost:5000/sfm -d '{"numero_processo":"0000002-78.2014.5.20.0007"}' -H 'Content-Type:application/json' -X POST)

# but if i store it in a variable then it doesn't work (when I 
# look at the request on a flask backend, the data parameter is empty)
# note that i've escaped the double quotes
command="curl -s http://localhost:5000/sfm -d '{\"param\":\"8.2014.5.20.0007\"}' -H 'Content-Type:application/json' -X POST"
out=$($command)

这两种方法不应该完全相同吗?这是怎么回事?

0 个答案:

没有答案