[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

Re: Test existence of shell variable, bash, csh



On Sat, May 05, 2001 at 04:35:02PM -0700, Karsten M. Self wrote:
> > 
> > Some bourne shells don't like testing variables that don't exist yet.
> 
> That's precisely what we're trying to test.

where you run in to trouble really is when you don't quote the
variable, for example:

if [ $FOO = bar ] ; then
	echo true
fi

if $FOO is unset you get a shell error about unary operator.  because
the above translates to:

if [ = bar ] ; then
	echo true
fi

which is not valid.

if you quite the variable you get:

if [ "" = bar ] ; then
	echo true
fi

the quotes of course are removed, but they show here that an actual
null is being tested for equality with `bar'.  

-- 
Ethan Benson
http://www.alaska.net/~erbenson/

Attachment: pgpGEZYovivgB.pgp
Description: PGP signature


Reply to: