This has been asked several times, with several accepted answers, but, on my trials none of the answers seem to work... I have two arrays, each of which represent the parameter list for a command. As such, I want to quote the strings properly to use with eval:
bash-4.2> ARRAY0=()
bash-4.2> ARRAY3=("ONE" "TWO WITH SPACE" "THREE")
bash-4.2> echo cmd $opt_arg $(printf "%q " "${ARRAY0[@]}")
cmd ''
bash-4.2> echo cmd $opt_arg $(printf "%q " "${ARRAY3[@]}")
cmd ONE TWO\ WITH\ SPACE THREE
bash#
Where $opt_arg may or may not be populated. The problem is that in the first case, where the array is empty, it outputs '' as a parameter, even though the array is empty. This kills my command, as it's expecting zero arguments. I've not found a neat solution (I can do an if [[ ${#ARRAY0[@]} ]] around it, but that's rather ugly...). Is there a neat way to do this?
dashtag doing in a question about arrays? dash doesn't support arrays at all (nor does it supportprintf '%q').[[ ${#array[@]} ]]doesn't work -- it checks for whether your string is empty.0is no more an empty string than1is.echo "cmd $opt_arg $( (( ${#array[@]} )) && printf '%q ' "${array[@]}")"opt_argisn't part of the array, or even a separate array? That way you could have multiple optional arguments if you wanted to without needing unquoted expansion).