命令中的Shell变量替换

Vik*_*s J 2 bash scripting

我怎样才能使下面的命令工作.

export CURDATE=`date +%Y-%m-%d`
curl -XPOST "http://localhost:9200/test/type" \
  -d ' { "AlertType": "IDLE", "@timestamp": $CURDATE }'
Run Code Online (Sandbox Code Playgroud)

我收到错误"原因":"无法识别的令牌'$ CURDATE':期待"我如何在上面的代码中得到正确的变量替换

and*_*lrc 5

单引号不会扩展变量,使用双引号:

curdate=$(date +'%Y-%m-%d')
curl -XPOST "http://localhost:9200/test/type" \
  -d '{"AlertType": "IDLE", "@timestamp": "'"$curdate"'"}'
Run Code Online (Sandbox Code Playgroud)

我还在扩展中添加了JSON引号,因此它变成了:

{"AlertType": "IDLE", "@timestamp": "2016-05-23"}
Run Code Online (Sandbox Code Playgroud)

不应该有任何导出变量的需要.通常只有环境变量写入全部大写.最后我将命令替换改为$(...)

'{"AlertType": "IDLE", "@timestamp": "'"$curdate"'"}'
#                                    ^^
#                                    |End singlequotes
#                                    JSON quote
Run Code Online (Sandbox Code Playgroud)