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

BashCurl

Bash Problem Overview


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?

Bash Solutions


Solution 1 - Bash

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

Solution 2 - Bash

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

Solution 3 - Bash

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.

Solution 4 - Bash

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

Solution 5 - Bash

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

Solution 6 - Bash

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

Solution 7 - Bash

easiest way :

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionChernoffView Question on Stackoverflow
Solution 1 - BashmjuarezView Answer on Stackoverflow
Solution 2 - BashMatt ClarkView Answer on Stackoverflow
Solution 3 - BashChernoffView Answer on Stackoverflow
Solution 4 - BasheaorakView Answer on Stackoverflow
Solution 5 - BashSeraltoView Answer on Stackoverflow
Solution 6 - BashShehan WeerasooriyaView Answer on Stackoverflow
Solution 7 - BashSaurav KumarView Answer on Stackoverflow