The old advice used to be to double-quote any expression involving a
$VARIABLE, at least if one wanted it to be interpreted by the shell as one single item, otherwise, any spaces in the content of
$VARIABLE would throw off the shell.
I understand, however, that in more recent versions of shells, double-quoting is no longer always needed (at least for the purpose described above). For instance, in
% FOO='bar baz' % [ $FOO = 'bar baz' ] && echo OK bash: [: too many arguments % [[ $FOO = 'bar baz' ]] && echo OK OK % touch 'bar baz' % ls $FOO ls: cannot access bar: No such file or directory ls: cannot access baz: No such file or directory
zsh, on the other hand, the same three commands succeed. Therefore, based on this experiment, it seems that, in
bash, one can omit the double quotes inside
[[ ... ]], but not inside
[ ... ] nor in command-line arguments, whereas, in
zsh, the double quotes may be omitted in all these cases.
But inferring general rules from anecdotal examples like the above is a chancy proposition. It would be nice to see a summary of when double-quoting is necessary. I'm primarily interested in