I've got a shell script with a section like this:
if [ "$condition" -eq 1 ]; then
external_command -argument value1
else
external_command -argument value1 -second_argument value2
fi
I was bugged by this repetition, so I tried this:
arg="-second_argument"
val="value2"
if [ "$condition" -eq 1 ]; then
arg=""
val=""
fi
external_command -argument value1 "$arg" "$val"
It didn't work because the external_command still gets the empty strings as distinct arguments and complains.
Is there a way to do this without building the command line repeatedly? In my actual code there are 4 different conditions, so there's a lot of needless repetition.
If I were using Bash, I'd follow Bash FAQ 050 and build an array for the command arguments, but I'm not.
valdoes not contain spaces, you could simply leave out the quotes, but in general, I don't think this can be done in Pure Posix shell, without reverting to dirty tricks usingeval.bashorzsh, as both have arrays.