1

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?

1 Answer 1

2

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"`
Sign up to request clarification or add additional context in comments.

2 Comments

Nice (ab)use of eval. +1.
Preferred way is to use BASH and avoid eval :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.