问题
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