Sample Header Ad - 728x90

Why does -n with unquoted variable containing empty string return true?

1 vote
1 answer
129 views
From man bash:
-n string
    True if the length of string is non‐zero.
Examples:
# expected
$ var=""; [ -n "$var" ]; echo $?
1

# unexpected?
$ var=""; [ -n $var ]; echo $?
0
Here we see that -n with unquoted variable containing empty string returns true. Why? Why is $var required to be quoted here?
Asked by pmor (665 rep)
Dec 28, 2023, 10:26 AM
Last activity: Dec 28, 2023, 04:35 PM