jdf
jdf

Reputation: 739

Why is [ -n $var ] true on empty string?

Code:

#!/bin/bash

EXISTS=""

echo ${#EXISTS} #print size of string

if [ -n $EXISTS ]; then
    echo "It exists!"
else
    echo "It is empty."
fi

if [ -z $EXISTS ]; then
    echo "It is empty."
else
    echo "It exists!"
fi

Output:

0
It exists!
It is empty.

From man bash:

-z string

True if the length of string is zero.

-n string

True if the length of string is non-zero.

Could someone explain this behavior of -n to me, and why it doesn't agree with -z? Thanks!

Upvotes: 2

Views: 132

Answers (2)

Keith Thompson
Keith Thompson

Reputation: 263467

EXISTS=""
if [ -n $EXISTS ]; then
    ...
fi

The behavior of [ ... ] depends on the number of arguments before the closing ].

If there's exactly one argument, it tests whether that argument is non-empty. Since the string "-n" is non-empty, [ -n ] is true. -n is interpreted as an operator only if it's followed by another argument.

To fix this, quote the argument so it's interpreted as a single empty argument, rather than as no argument:

if [ -n "$EXISTS" ]; then
    ...
fi

Upvotes: 3

anubhava
anubhava

Reputation: 785471

Quote the variable or better use [[...]] in BASH:

if [[ -n $EXISTS ]]; then echo "It exists!"; else echo "It is empty."; fi

It will print:

It is empty.

Similarly:

if [ -n "$EXISTS" ]; then echo "It exists!"; else echo "It is empty."; fi
It is empty.

Upvotes: 4

Related Questions