0

I have a shell script inside of which I call a command with several arguments. When I call the command from the terminal, the proper syntax is:

command --foo='bar baz'

Inside my script, I want to have --foo='bar baz' inside a variable $ARGS, so that I can put

command $ARGS

in my script. However, if I simply assign $ARGS="--foo='bar baz'" then the string 'bar gets passed as the parameter for foo instead of the complete string bar Every combination of quotes/escaped quotes that I've tried has resulted in either nothing being passed, or just'bar` or something similar. I've looked into parameter expansion in bash but am not quite sure how it works.

I would just copy/paste the entire command and its arguments instead of using variables but I call the command in multiple places in the script and want to minimize code re-use, as the complete list of arguments is several lines long.

marked as duplicate by G-Man, Jeff Schaller bash Mar 16 at 12:53

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • 1
    simply ARGS='--foo=bar baz' should do if there is a single argument; or set -- '--foo=bar baz' '--quux=x y'; command "$@" if there are multiple arguments. If it doesn't, please post a reproducible script snippet. – mosvy Mar 16 at 2:40
  • 1
    And since your question is tagged bash, you can also use arrays: args=('--foo=bar baz' '--quux=x y'); command "${args[@]}". But you cannot pass arrays through environment variables. – mosvy Mar 16 at 2:45
  • @mosvy decided to go with the array approach which works beautifully, thanks! – theasianpianist Mar 16 at 3:26