Search code examples
bashescapingexecquotesdouble-quotes

How do I 'exec' a bash command like this where an option is multi-word?


On the commandline I can run my binary this way:

theBinary_exe -q 'more than one word' -f foo -b bar

When I put that in a bash script and run the script it's all fine. But if I do this in the script:

CMD="theBinary_exe -q 'more than one word' -f foo -b bar"
exec $CMD

The 'q' argument only passes the first word to the executable. I tried using \" as well with no luck. Is there a wrapping I can use to prevent that when I use exec?


Solution

  • Do not use a regular parameter.

    Use an array:

    args=( -q 'more than one word' -f foo -b bar)
    theBinary_exe "${args[@]}"