NULL checking

Hi I need to check the output of a string to null. For eg the output I get for $KIT is empty. So want to echo something when the output is empty.
How can I do that?

Thanks in advance

if [ "X$KIT" =="X" ] ; then
   echo Kit is undefined
fi

Or:

[ -z "$KIT" ] && your code when empty

Just [ "$KIT" ] should also work I guess!?

Not if kit=0

Yeah. But in that case its not "null".

Yep,
with most modern shells.

It may not work with the old Bourne shell in some corner cases:

bash-2.03$ uname -sr
SunOS 5.8
bash-2.03$ sh -c 'v=-d; [ "$v" ] && echo ok'
sh: test: argument expected
bash-2.03$ bash -c 'v=-d; [ "$v" ] && echo ok'
ok
bash-2.03$ ksh -c 'v=-d; [ "$v" ] && echo ok'
ok

See bit.ly/pjR2c6 for more:

The two commands:

  test "$1" 
  test ! "$1"    

could not be used reliably on some historical systems. Unexpected results would occur if such a string expression 
were used and $1 expanded to '!', '(', or a known unary primary. Better constructs are:

  test -n "$1"
  test -z "$1" 
1 Like