Using environment variables in cURL - Unix command

My question is very simple. I want to use environment variables in a cURL command similar to this:

curl -k -X POST -H 'Content-Type: application/json' -d '{"username":"$USERNAME","password":"$PASSWORD"}' 

When I run the command, $ USERNAME is passed to the command as the string "$ USERNAME", not the value of the variable. Is there any way to avoid this situation?

Thanks.

+4
source share
2 answers

Single quotes prevent variable substitution, so use double quotes. Then the inner double quotes must be hidden.

 ... -d "{\"username\":\"$USERNAME\",\"password\":\"$PASSWORD\"}" 
+8
source

For less citation, read standard input instead.

 curl -k -X POST -H 'Content-Type: application/json' -d @- <<EOF { "username": "$USERNAME", "password": "$PASSWORD"} EOF 

-d @foo is read from a file named foo . If you use - as a file name, it reads from standard input. Here, standard input is provided from the document here, which is treated as a string with two quotation marks without actually including it in double quotation marks.

+2
source

All Articles