command ${VALUE:+--flag "$VALUE"}
This runs command
with the option --flag
and its option-argument "$VALUE"
if the variable VALUE
is defined and non-empty. If the variable is empty or undefined, it runs command
without arguments.
In the zsh
shell, which does not perform word-splitting on unquoted expansions, you will have to set the SH_WORD_SPLIT
shell option (setopt SH_WORD_SPLIT
), run the shell in sh
compatibility mode (zsh --emulate sh
), or use
command ${=VALUE:+--flag "$VALUE"}
... to ensure that --flag
and "$VALUE"
are passed as two separate arguments when VALUE
is set to a non-empty value.
Remove the :
in the parameter substitution if you want the --flag
option added when VALUE
is defined but empty.