0

I want to pass multiple arguments through to curl. Some of these arguments are quoted and contain spaces.

I have tried like this:

ARGS="http://example.org -H 'My-Header: Foo'"
curl -vvv $ARGS

But my header is not set and I get an error at the end curl: (6) Could not resolve host: Foo'.

I have also tried quoting ARGS like this:

ARGS="http://example.org -H 'My-Header: Foo'"
curl -vvv "$ARGS"

But I get curl: (3) URL using bad/illegal format or missing URL.

If I just run curl with the arguments directly, then it works fine:

curl -vvv http://example.org -H 'My-Header: Foo'

How can I pass these arguments through to curl correctly?

Rupert Madden-Abbott
  • 12,899
  • 14
  • 59
  • 74
  • 2
    Store the arguments in an array; see [this question](https://stackoverflow.com/questions/12136948/why-does-shell-ignore-quoting-characters-in-arguments-passed-to-it-through-varia). (Please ignore all suggestions to use `eval` -- it's a huge footgun.) – Gordon Davisson Aug 06 '21 at 09:34

1 Answers1

0

There is a command called eval which evaluates all the arguments into one string and then runs it as a one big command.

Try eval curl $ARGS

I recommend you to checkout eval's man page ;)

adamito
  • 19
  • 1
  • Please don't ever recommend `eval` without any sort of warning about its potential to cause problems. `eval` makes the shell parsing process even more confusing than it normally is, and the dividing line between data and executable code even blurrier. It *is* possible to use `eval` safely, but it requires a very good understanding of the shell parsing process; here, you're essentially recommending it as a substitute for understanding shell parsing, and that's just a recipe for disaster. – Gordon Davisson Aug 06 '21 at 22:11