shell variable curl command

How to Pass Variable in cURL Command

cURL is a popular Linux command that allows you to download files & data from URLs. It is used by many developers and system administrators to transfer files & data. But sometimes you may need to pass a shell variable in your cURL command’s URL. This can be confusing for beginners since adding $ sign in the beginning of variable within cURL command will give you an error. In this article, we will learn how to pass variable in cURL command.


How to Pass Variable in cURL Command

Let us say you want to run the following cURL command, with job_id shell variable in it.

curl -u ${USER_ID}:${PASSWORD} -X GET 'http://www.example.com/job-details/${job_id}'

Typically, we need to use shell variables as ${variable_name} in cURL command but the above command will give an error.

{"message":"Sorry. An unexpected error occured.", "stacktrace":"Bad Request. The request could not be understood by the server due to malformed syntax."}

This is because cURL is unable to parse ${job_id} properly. Let us say your shell variable job_id has value 1000, if try running the following cURL command, replacing ${job_id} with 1000, you will find that it works properly.

curl -u ${USER_ID}:${PASSWORD} -X GET 'http://www.example.com/job-details/1000'

This is because when we use variables in shell, only the variables in double quotes are expanded but the variables in single quotes are not expanded. So in this case, the trick is to simply replace single quotes around the URL, with double quotes.

curl -u ${USER_ID}:${PASSWORD} -X GET "http://www.example.com/job-details/${job_id}"

Here is another example, similar the one above, where we set shell variable and call it in cURL command.

job_id=150;curl -u ${USER_ID}:${PASSWORD} -X GET 'http://www.example.com/job-details/${job_id}'

Similarly, here is another example where we use multiple shell variables by enclosing them within double quotes, instead of enclosing them within curly braces. Each of the variable names starting with $ is a shell variable defined previously.

userdetails="$username:$apppassword"
base_url_part='https://example.com/repositories'
path="/$teamName/$repoName/downloads/$filename"
base_url="$base_url_part$path"**strong text**
curl  -L -u "$userdetails" "$base_url" -o "$downloadfilename"

In the above command, we use shell variables by enclosing them within double quotes instead of curly braces.

So far, we have looked at how to pass shell variable in cURL command’s requested URL and option values.

Now let us look at a slightly different example of how to use shell variables while passing data to 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.

curl -d "{\"query\":\"$name\", \"turnOff\":true}" ...

The next option is to enclose the entire argument in single quotes but in this case, we need to separately enclose the shell variable in “‘” (single quote within double quotes).

curl -d '{"query":"'"$name"'", \"turnOff\":true}' ...

In this article, we have learnt how to call cURL command using shell variables. This is useful if you want to call cURL command from within shell script and use pre-defined shell variables in it. This makes it easy to make cURL more dynamic. For example, you can use this method to dynamically construct URLs in your shell script. In fact, you can even use it in a loop to construct multiple dynamic URLs in each cURL command call.

Also read:

Shell Script to Read Data from Text File using For Loop
Awk Split One Column into Multiple Columns
How to Read Variable From File in Shell
Shell Script to Print Output to Table Format
How to Change PHP Version in Ubuntu

One thought on “How to Pass Variable in cURL Command

Leave a Reply

Your email address will not be published. Required fields are marked *