0

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?

3

1 Answer 1

5

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
1
  • Wow, what a great edge case test. Commented Jul 16, 2018 at 2:16

Not the answer you're looking for? Browse other questions tagged or ask your own question.