Reputation: 134
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
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