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 ?


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.

Need Your Help

Moving a list of ints to the front of a list

c# linq list sorting

I have a List of Int64 (List A) that needs to moved infront of another List (Int64) (List B).

OpenCV : convert the pointer in memory to image

c++ c image pointers opencv

I have a grabber which can get the images and show them on the screen with the following code