[CALUG] Shell script question.

Etan Reisner deryni at unreliablesource.net
Thu Mar 4 20:01:39 EST 2010


On Wed, Mar 03, 2010 at 06:00:47PM -0500, Jim Bauer wrote:
> Brian Debelius wrote:
<snip>
> >     Why would you do this:
> >
> > if [ "x$var" = "x" ]; then
> > ...
> > fi
> >
> >    Instead of this:
> >
> > if [ -z $var ]; then
> > ...
> > fi
>
> To protect against an error if var was something like '-a'.  Most modern
> shells don't get tripped up over things like that.  But older systems
> would generate an error.
>
> And your -z version above has a bug.  Try it with var="a b".
> Use "$var" not $var to fix it.

It also protected against issues with older shells and empty strings,
which similarly tends not to be an issue these days.

    -Etan




More information about the CALUG mailing list