CURL escape single quote

BashShellCurlElasticsearch

Bash Problem Overview


How can I make this work?

curl -XPOST 'http://localhost:9290/location/place' -d '{"geoloc": {"lat": "38.1899", "lon": "-76.5087"}, "longitude": "-76.5087", "admin_name1": "Maryland", "admin_name2": "St. Mary's", "admin_name3": "", "postal_code": "20692", "admin_code3": "", "country_code": "US", "admin_code1": "MD", "latitude": "38.1899", "admin_code2": "037", "accuracy": null, "place_name": "Valley Lee"}'

The ' in Mary's is causing this to fail. I am running it from a file like cat curl-cmd.txt | sh but it won't work from the command line either. I've tried using \' and \\' and \u0027 (the unicode ')

I'm stuck

Bash Solutions


Solution 1 - Bash

I had the same problem. The simplest solution is to escape the apostrophe with a backslash in addition to wrapping it in a set of single quotes. '\''

For your use case, change Mary's to Mary'\''s and it should work.

curl -XPOST 'http://localhost:9290/location/place' -d '{"geoloc": {"lat": "38.1899", "lon": "-76.5087"}, "longitude": "-76.5087", "admin_name1": "Maryland", "admin_name2": "St. Mary'\''s", "admin_name3": "", "postal_code": "20692", "admin_code3": "", "country_code": "US", "admin_code1": "MD", "latitude": "38.1899", "admin_code2": "037", "accuracy": null, "place_name": "Valley Lee"}'

> An alternate approach is to wrap the POST data (-d) in double quotes while escaping all nested occurrences of double quotes in the JSON string with a backslash.

curl -XPOST 'http://localhost:9290/location/place' -d "{\"geoloc\": {\"lat\": \"38.1899\", \"lon\": \"-76.5087\"}, \"longitude\": \"-76.5087\", \"admin_name1\": \"Maryland\", \"admin_name2\": \"St. Mary's\", \"admin_name3\": \"\", \"postal_code\": \"20692\", \"admin_code3\": \"\", \"country_code\": \"US\", \"admin_code1\": \"MD\", \"latitude\": \"38.1899\", \"admin_code2\": \"037\", \"accuracy\": null, \"place_name\": \"Valley Lee\"}"

Solution 2 - Bash

Rule Of Thumb: In case you want explicitly representing single quote or double quotes in your string on bash, Use backslash (\) depends on your String Wrapper (should be in the same type). The backslash (\) character is used to escape characters that otherwise have a special meaning, such as newline, backslash itself, or the quote character.

Examples:

-Double Quote Example - Use \"

in case you want to print on bash She said "Yes I Do"

echo "She said \"Yes I Do\""
#output:
She said "Yes I Do"

echo 'she said "Yes I Do"' 
#output:
She said "Yes I Do"

-Single Quote example - Use '\''

in case you want to print on bash My Daughter's dog likes cat treats

echo "My Daughter's dog likes cat treats"
#output:
My Daughter's dog likes cat treats

echo 'My Daughter'\''s dog likes cat treats' 
#output:
My Daughter's dog likes cat treats

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
QuestionmikebView Question on Stackoverflow
Solution 1 - BashTravis ClarkeView Answer on Stackoverflow
Solution 2 - BashavivamgView Answer on Stackoverflow