[CALUG] Shell script question.

Brian Debelius bdebelius at intelesyscorp.com
Wed Mar 3 14:05:57 EST 2010


   Looking at Bacula scripts and other scripts, I see a test for an 
empty string performed by adding an 'x' before the variable, and then 
comparing this to another string that is just an 'x'.

   The shell test function has a -z string test that returns true if the 
string is empty.

It appears to me that more people use the 'x' comparison.  Is there a 
technical reason why one would be preferable to the other?

    Why would you do this:

if [ "x$var" = "x" ]; then

   Instead of this:

if [ -z $var ]; then

More information about the CALUG mailing list