Re^2: Bourne Shell: Variable defined?

Chris Torek chris at mimsy.UUCP
Thu Mar 23 17:22:34 AEST 1989


In article <1605 at fig.bbn.com> rsalz at bbn.com (Rich Salz) writes:
>The trick of
>	[ "X$foo" = "X" ]
>doesn't work because unset variables evaluate to the null string.
>Sometimes you care, sometimes you don't.  For when you care, this
>seems maximally portable:
>
>	VARNAME_IS_SET=`set | grep VARNAME | wc -l` ...

You have to augment the grep pattern with anchors (^VARNAME$) or this
finds substrings.  There is a much better---simple and reliable---test
of which I was reminded recently:

	case ${var+X} in X) echo is set;; *) echo not set;; esac

or (equivalently, but slower in shells without a builtin test)

	if [ "${vax+X}" ]; then echo is set; else echo not set; fi
-- 
In-Real-Life: Chris Torek, Univ of MD Comp Sci Dept (+1 301 454 7163)
Domain:	chris at mimsy.umd.edu	Path:	uunet!mimsy!chris



More information about the Comp.unix.questions mailing list