nickbit
nickbit

Reputation: 134

How to quote a shell script command?

I am trying to form a parametric call to curl from a shell script

header_string="--header \"X-param1: 1\" --header \"X-param2: 2\""
url="http://example.com"
command="curl $header_string $url"

a=`${command}`

This does not work because it is interpreted like

curl --header '"X-param1:' '1"' --header '"X-param2:' '2"' http://example.com

How can I quote this correctly?

Upvotes: 1

Views: 83

Answers (1)

anubhava
anubhava

Reputation: 785068

If you're using BASH you can make good use of BASH arrays:

header_string=(--header "X-param1: 1" --header "X-param2: 2")
url="http://example.com"
command="$(curl -s "${header_string[@]}" "$url")"

But if you're using old sh then use of dreaded eval:

header_string="--header \"X-param1: 1\" --header \"X-param2: 2\""
url="http://example.com"
command=`eval "curl $header_string $url"`

Upvotes: 2

Related Questions