Path: utzoo!attcan!uunet!lll-winken!ames!mailrus!eecae!tank!mimsy!chris From: chris@mimsy.UUCP (Chris Torek) Newsgroups: comp.unix.questions Subject: Re: Re^2: Bourne Shell: Variable defined? Message-ID: <16505@mimsy.UUCP> Date: 23 Mar 89 07:22:34 GMT References: <1819@umbc3.UMBC.EDU> <9899@smoke.BRL.MIL> <2202@solo1.cs.vu.nl> <1605@fig.bbn.com> Organization: U of Maryland, Dept. of Computer Science, Coll. Pk., MD 20742 Lines: 21 In article <1605@fig.bbn.com> rsalz@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@mimsy.umd.edu Path: uunet!mimsy!chris