Friday, July 22, 2022

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

Issue

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[@]}"


Answered By - chepner
Answer Checked By - Mildred Charles (PHPFixing Admin)

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.