This question already has an answer here:
I have a script that fails to detect zero length strings,the script uses
[ -n $value ] in a bash conditional expression, i.e.
#!/usr/bin/env bash value="" if [ -n $value ] then echo "value is non-zero" fi
value is non-zero
If I use
[[ -n $value ]] it works, i.e.
#!/usr/bin/env bash value="" if [[ -n $value ]] then echo "value is non-zero" fi
[[ produces no output as expected. From the man page:
[[ expression ]] Return a status of 0 or 1 depending on the evaluation of the conditional expression expression. Expressions are composed of the pri‐ maries described below under CONDITIONAL EXPRESSIONS. Word splitting and pathname expansion are not performed on the words between the [[ and ]]; tilde expansion, parameter and variable expansion, arithmetic expansion, command substitution, process substitution, and quote removal are performed. Conditional operators such as -f must be unquoted to be recognized as primaries.
I can't make out an explanation for the behaviour from this.
[[ detect zero length strings but