Bourne Shell: Variable defined?

Doug Gwyn gwyn at smoke.BRL.MIL
Wed Mar 22 14:38:30 AEST 1989


In article <1819 at umbc3.UMBC.EDU> rostamia at umbc3.UMBC.EDU (Rouben Rostamian) writes:
>In C Shell the variable $?foo returns true or false (0 of 1, really)
>depending on whether or not the variable "foo" has been already defined.
>What is the equivalent construction in Bourne Shell?  In other words,
>how can I tell, in Bourne Shell, whether the variable "foo" exits.

Usually all you need is
	if [ X"$foo" = X ]
	then	:	# doesn't exist or is an empty string
	else	:	# exists as a nonempty string
	fi
The X is to avoid problems if $foo starts with a dash etc.
If that's not a problem then
	if [ "$foo" ]
	then	:	# nonempty
	else	:	# empty or nonexistent
	fi
is a simpler test.



More information about the Comp.unix.questions mailing list