Thursday, August 09, 2007

Some quirky points when deal with shell 'test' program

Or when you use if [ -n "string"]; then ... in shell.

> test -n string
returns 1 if string is not empty and 0 otherwise.

> test -n
returns 1

> test -n ""
returns 0

This brings up an important point, you need to quote the string in shell otherwise 'test' will think there is no argument for empty string and returns 1 (wrong result).

if [ -n "$my_string" ]; then

No comments: