Chernoff
Chernoff

Reputation: 2622

How to include an '&' character in a bash curl statement

I'm trying to use curl in bash to download a webpage, but the & symbol in the URL isn't interpreted as a character as I would like. Any ideas on how I can convince bash that the symbol & is just a boring character and nothing special?

Upvotes: 142

Views: 145417

Answers (7)

Saurav Kumar
Saurav Kumar

Reputation: 970

easiest way :

curl -X GET -G http://example.com -d var1=$1 -d var2=$2

Upvotes: 3

Shehan Weerasooriya
Shehan Weerasooriya

Reputation: 822

Use the parameter --url with double quotes worked for me

Upvotes: 6

Seralto
Seralto

Reputation: 1076

You can use %26 instead &. The entity code will work fine.

Upvotes: 14

koders
koders

Reputation: 5804

Instead of trying the escape "&" characters, you can specify http url parameters in POST requests with -d parameter as shown below:

curl -X POST http://www.example.com \
-d arg1=this \
-d arg2=that

If the request is a GET, then you should also add -G option, which tells curl to send the data with GET request.

curl -X GET -G http://www.example.com \
-d arg1=this \
-d arg2=that

Upvotes: 25

Matt Clark
Matt Clark

Reputation: 28629

curl "http://www.example.com?m=method&args=1"

Are you using the & as a delimiter for a GET URL? Or is in a piece of data?

If it is in data you must encode it to an HTML character, if not, surround with quotes.

The encoding for & should become %26 in the URL.

curl "http://www.example.com?m=this%26that

Upvotes: 64

Chernoff
Chernoff

Reputation: 2622

Putting single quotes around the & symbol seems to work. That is, using a URL like http://www.example.com/page.asp?arg1=${i}'&'arg2=${j} with curl returns the requested webpage.

Upvotes: 33

mjuarez
mjuarez

Reputation: 16844

Putting the entire URL inside double quotes should take care of your problem.

Upvotes: 184

Related Questions