Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using Environment Variables in cURL Command - Unix

My question is very simple. I want to use environment variables in a cURL command sth 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 a "$USERNAME" string not the value of the variable. Is there a way to escape this situation?

Thanks.

like image 969
leventunver Avatar asked Aug 12 '15 11:08

leventunver


People also ask

How do you curl environment variables?

Download the curl for 64-bit or 32-bit package based on your operating system. A zip package is downloaded that contains all the files of the utility. Unzip all the files to a folder on the Windows machine. Press the Windows button on the keyboard and type Edit the system environment variables in the search box.

How do you pass variables in curl command?

Let us say you want to pass shell variable $name in the data option -d of cURL command. There are two ways to do this. First is to put the whole argument in double quotes so that it is expandable but in this case, we need to escape the double quotes by adding backslash before them.


1 Answers

Single quotes inhibit variable substitution, so use double quotes. The inner double quotes must then be escaped.

...  -d "{\"username\":\"$USERNAME\",\"password\":\"$PASSWORD\"}"
like image 143
glenn jackman Avatar answered Sep 22 '22 04:09

glenn jackman