1

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?

1 Answers1

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[@]}"
kmkaplan
  • 614
  • 4
  • 6