How can I use environment variables in body of a curl PUT request?

£可爱£侵袭症+ 提交于 2019-12-24 20:30:08

问题


I want to do a curl request which uses environment variables in the body:

curl -XPUT http://${HOST}/create -d'{"user":"${USER}"}'

In this request, ${HOST} is correctly replaced by the environment variable, but ${USER} is not. How can I replace ${USER} as well?


回答1:


Shell parameter expansion doesn't take place within single quotes. You can close the single quotes and start new double-quotes for the expansion:

curl -XPUT http://${HOST}/create -d'{"user":"'"${USER}"'"}'

Or you could use double-quotes instead:

curl -XPUT http://${HOST}/create -d"{\"user\":\"${USER}\"}"

In both cases, you need to be able to trust that $USER doesn't contain " - you might want to sanitize it first:

# This is a Bash extension to POSIX
USER=${USER//\"}


来源:https://stackoverflow.com/questions/48867599/how-can-i-use-environment-variables-in-body-of-a-curl-put-request

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!