使用bash变量将多个标头传递给curl命令 [英] Using a bash variable to pass multiple headers to curl command

查看:129
本文介绍了使用bash变量将多个标头传递给curl命令的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想发出带有多个标头的 curl 请求。解决方案是发出以下命令:

I would like to make curl request with multiple headers.The solution would be to make this command :

curl -H "keyheader: value" -H "2ndkeyheader: 2ndvalue" ...

我的目标是仅对所有标头使用一个变量:

My goal is to use only one variable with all the headers like :

headers='-H "keyheader: value" -H "2ndkeyheader: 2ndvalue" '
curl $headers

to发送

curl -H "keyheader: value" -H "2ndkeyheader: 2ndvalue"

当前,问题是:我可以使用' 声明我的字符串,但是 bash 尝试运行-H 之后的内容参数和答案:

Currently, the problem is : I can use ' or " to declare my string, but bash tries to run what's after "-H" as arguments and then answers :

command unknown

想知道这里出了什么问题。

Would like to know what is going wrong here.

推荐答案

您只需要使用数组和 not 变量以传递引号字符串。

You just need to use an array and not a variable to pass the quoted strings.

declare -a curlArgs=('-H' "keyheader: value" '-H' "2ndkeyheader: 2ndvalue")

现在以这种方式完全传递此数组,数组扩展(带双引号)将处理

and now pass this array fully that way, the array expansion(with double-quotes) takes care of the arguments within double-quotes to be not split while passing.

curl "${curlArgs[@]}"

有关为何将args放入变量失败的更多信息,请参见 BashFAQ / 050-我试图将命令放入变量中,但复杂的情况总是会失败!

For more insight into why putting the args in your variables fail, see BashFAQ/050 - I'm trying to put a command in a variable, but the complex cases always fail!

这篇关于使用bash变量将多个标头传递给curl命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆