Manticore
Manticore

Reputation: 309

string check in bash with -z and -n

I made a mistake in test syntax in bash but now I want to understand what really happens in string check with -n and -z. I wrote the following lines to get in LINENUM variable the line number from a grep. When the string is not found (there is only one in the file, for sure), the LINENUM variable is empty.

$ LINENUM=$(grep -w -n mystring myfile | cut -d: -f1)
$ echo --$LINENUM--
----
$ if [ -n $LINENUM ] ; then echo "Checked -n"; fi
Checked -n
$ if [ -z $LINENUM ] ; then echo "Checked -z"; fi
Checked -z

Then I realized I forgot the double quotes and then the following check gave to me:

$ if [ -n "$LINENUM" ] ; then echo "Checked -n"; fi
$ if [ -z "$LINENUM" ] ; then echo "Checked -z"; fi
Checked -z

So, in the former tests, where I forgot the double quotes, versus what did the if test check , really, since I got two positive checks from both -n and -z ?

Upvotes: 3

Views: 668

Answers (1)

chepner
chepner

Reputation: 532303

Without the quotes, your test statement (with either operator, represented with -X here), reduces to

if [ -X ]; then echo "Checked -X"; fi

According to the POSIX standard, the one-argument form of test (which you now have here) is true if the argument is non-null. Since the literal string -X is non-null (it's not an operator anymore), it evaluates to true.

With the quotes, you get

if [ -X "" ]; then echo "Checked -X"; fi

Since the quotes force an empty 2nd argument, you have a 2-argument form of test, and -X (whether -n or -z) is properly recognized as a primary operator acting on the 2nd, null, argument.

Upvotes: 4

Related Questions