[[ -z $var ]]
can be a bit "safer" in regards to quoted variables. Works in bash and KSH.Edit to add: http://stackoverflow.com/questions/669452/is-preferable-over...
[[ -z $var ]]
can be a bit "safer" in regards to quoted variables. Works in bash and KSH.Edit to add: http://stackoverflow.com/questions/669452/is-preferable-over...
[ -z "$var" ]
This has literally no possibility of failing in any weird way, no matter which
Bourne or Korn shell descendant you use.you can even go further to do "${bar}", this is even safer I feel.
Most of the time, whatever is in [[ ]] can be easily emulated with POSIX/SUS-compatible tools and most of the time it doesn't matter that the tool is not a shell built-in.
Let's take people's favourite =~ operator. In bash, according to documentation, it uses ERE syntax. In zsh, it is ERE or PCRE, depending on an option being set. This is a subtle but important difference in how [[ ]] works. And then again, your environment can change the behaviour of a script you're running, which is (a) unexpected and (b) undesirable, and the change in the behaviour can range from "doesn't find thing it should" to "crashes" to "goes awry in an unexpected way".
[ -z "${var-}" ]
works with +u or -u> %q ARGUMENT is printed in a format that can be reused as shell
> input, escaping non-printable characters with the proposed POSIX
> $'' syntax.