如何在 bash curl 语句中包含“&”字符
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13339469/
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
How to include an '&' character in a bash curl statement
提问by Chernoff
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 中使用 curl 来下载网页,但&
URL 中的符号没有像我希望的那样解释为字符。关于如何让 bash 相信该符号&
只是一个无聊的角色而没有什么特别之处的任何想法?
采纳答案by Chernoff
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.
在&
符号周围加上单引号似乎有效。也就是说,使用类似http://www.example.com/page.asp?arg1=${i}'&'arg2=${j}
curl的 URL 会返回请求的网页。
回答by mjuarez
Putting the entire URL inside double quotes should take care of your problem.
将整个 URL 放在双引号内应该可以解决您的问题。
回答by Matt Clark
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?
您是否使用 & 作为 GET URL 的分隔符?或者是在一段数据中?
If it is in data you must encode it to an HTML character, if not, surround with quotes.
如果它在数据中,您必须将其编码为 HTML 字符,如果不是,则用引号括起来。
The encoding for &
should become %26
in the URL.
编码&
应该%26
在 URL 中。
curl "http://www.example.com?m=this%26that
回答by noego
Instead of trying the escape "&" characters, you can specify http url parameters in POST requests with -d parameter as shown below:
您可以使用 -d 参数在 POST 请求中指定 http url 参数,而不是尝试转义“&”字符,如下所示:
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.
如果请求是 GET,那么您还应该添加 -G 选项,它告诉 curl 使用 GET 请求发送数据。
curl -X GET -G http://www.example.com \
-d arg1=this \
-d arg2=that
回答by Seralto
You can use %26
instead &
.
The entity code will work fine.
您可以%26
改用&
. 实体代码将正常工作。