Is there a difference between these two options in Bash:
# 1
if [[ "$VAR" ]]; then
#2
if [[ -n "$VAR" ]]; then
If not, are there situations that really require the -n
option?
Is there a difference between these two options in Bash:
# 1
if [[ "$VAR" ]]; then
#2
if [[ -n "$VAR" ]]; then
If not, are there situations that really require the -n
option?
Since [[
is smarter than test
about variables there are essentially no situations that require -n
.
$ foo=-n
$ [[ $foo ]] ; echo $?
0
$ [[ -n ]] ; echo $?
bash: unexpected argument `]]' to conditional unary operator
bash: syntax error near `]]'
$ foo="-z bar"
$ [[ $foo ]] ; echo $?
0
$ [ $foo ] ; echo $?
1