Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to pass a variable in a curl command in shell scripting

Tags:

bash

shell

curl

I have a curl command:

curl -u ${USER_ID}:${PASSWORD} -X GET 'http://blah.gso.woo.com:8080/rest/job-execution/job-details/${job_id}' 

The variable job_id has a value in it, say, 1160. When I execute the curl command in shell it gives me the following error:

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

If I pass the number '1160' directly in the command, as shown below, the curl command works.

curl -u ${USER_ID}:${PASSWORD} -X GET 'http://blah.gso.woo.com:8080/rest/job-execution/job-details/1160' 

I want to be able to pass the value of the variable in the curl command.

like image 484
user1452759 Avatar asked Nov 12 '12 10:11

user1452759


People also ask

How do you pass a variable to the curl command in shell?

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.

How do you set a variable in curl?

The Curl language naming conventions for variables suggest using lowercase letters and a hyphen to join multiple words (for example, variable-name). is the data type of the variable or constant. Specify any valid data type. type is required if value is not specified.

How do you pass variables in curl command in header?

A pretty good rule of thumb is to put double quotes around any variable you want to expand like curl "$header" -w "http code: %{http_code}\\n" -X POST -d "$BODY" "$URL" . Bash always expands $SOMETHING variables if they appear on their own, or if they appear in double quotes. (Not if they appear in single quotes).

Can you pass variables in shell script?

In a shell script, you can pass variables as arguments by entering arguments after the script name, for example ./script.sh arg1 arg2 . The shell automatically assigns each argument name to a variable. Arguments are set of characters between spaces added after the script.


1 Answers

When using variables in shell, you can only use doubles quotes, not single quotes : the variables inside single quotes are not expanded. Learn the difference between ' and " and `. See http://mywiki.wooledge.org/Quotes and http://wiki.bash-hackers.org/syntax/words

like image 154
Gilles Quenot Avatar answered Sep 20 '22 15:09

Gilles Quenot