1

This question already has an answer here:

I would like to run a command in a bash script with arguments however the arguments are conditional. What I would like to achieve is this

command --myflag var="value with space"

In the case above I would like to have --myflag var="value with space" conditional and it also has to have the double-quotes because of the space (among other reasons). So I attempted something like this

if [ $somecondition ]; then
  FLAG="--myflag var=\"value with space\""
fi

command $FLAG

When I run the above in debug I can see that is run is this

command --myflag 'var="value' with 'space"'

Notice the single quotes. I dont know why this is but after several attempts at trying this I seem to fail to find a solution,

How would one go about solving this?

marked as duplicate by Gilles bash Jan 19 '17 at 23:16

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.

3

You will have to use Bash array extension. Something like:

declare -a FLAG
if [ $somecondition ]; then
  FLAG=(--myflag 'var="value with space"')
fi
command "${FLAG[@]}"
  • Thanks you. Took a little while to figure out the correct syntax but bash arrays worked – ByteFlinger Jan 27 '17 at 9:36

Not the answer you're looking for? Browse other questions tagged or ask your own question.