You could try
java -jar compiler.jar ${externs:+--externs} "${externs:-}" --foo "another" --bar "more"
explanation, from bash docs
${parameter:-word}
Use Default Values. If parameter is unset or null, the expansion of word is substituted. Otherwise, the value of parameter is substituted.
${parameter:+word}
Use Alternate Value. If parameter is null or unset, nothing is substituted, otherwise the expansion of word is substituted.
You could try it yourself with:
externs="some value" printf '%s\n' ${externs:+--externs} "${externs:-}"
output
--externs
some value
Note: I used printf here test the case of $externs containing multiple words, where if the quoting wasn't being applied after the variable expansion, the output would look like
--externs
some
value
So I believe that should suffice.
externs= echo ${externs:+--externs} "${externs:-}"
output: nil
externs='two words'; if [ ! -z "$externs" ]; then externs_option="--externs \"$externs\""; fi; printf '%q\n' java -jar compiler.jar $externs_option --foo "two words" --bar "etc"-- and compare thetwo wordspassed alongside--externsand that passed alongside--foo.