bash CURL 转义单引号

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/32122586/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 22:04:27  来源:igfitidea点击:

CURL escape single quote

bashshellcurlelasticsearch

提问by mikeb

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'sis causing this to fail. I am running it from a file like cat curl-cmd.txt | shbut it won't work from the command line either. I've tried using \'and \\'and \u0027(the unicode ')

'Mary's引起此故障。我正在从类似的文件中运行它,cat curl-cmd.txt | sh但它也无法从命令行运行。我试过使用\'and\\'\u0027(unicode ')

I'm stuck

我被卡住了

回答by Travis Clarke

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'sto Mary'\''sand it should work.

对于您的用例,更改Mary'sMary'\''s它应该可以工作。

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.

另一种方法是将 POST 数据 ( -d) 括在双引号中,同时使用反斜杠转义 JSON 字符串中所有嵌套的双引号。

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\"}"

回答by avivamg

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.

经验法则如果您想在 bash 上的字符串中明确表示单引号或双引号,使用反斜杠 ( \) 取决于您的字符串包装器(应该是相同的类型)。反斜杠 ( \) 字符用于转义具有特殊含义的字符,例如换行符、反斜杠本身或引号字符。

Examples:

例子

-Double Quote Example- Use \"

-双引号示例-Use \"

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

如果您想在 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 '\''

-单引号示例-Use '\''

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

如果您想在 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