maart@cs.vu.nl (Maarten Litmaath) (03/23/89)
gwyn@smoke.BRL.MIL (Doug Gwyn ) writes: \In article <1819@umbc3.UMBC.EDU> rostamia@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. \ if [ X"$foo" = X ] \ then : # doesn't exist or is an empty string The question was: "Has it been defined?" Ergo, if $foo is an empty string, it IS defined and your test fails. -- Modeless editors and strong typing: |Maarten Litmaath @ VU Amsterdam: both for people with weak memories. |maart@cs.vu.nl, mcvax!botter!maart
gsf@ulysses.homer.nj.att.com (Glenn Fowler[drew]) (03/23/89)
# determine if var is set in Bourne-based shells case ${var+1} in 1) echo var set ;; *) echo var not set ;; esac -- Glenn Fowler (201)-582-2195 AT&T Bell Laboratories, Murray Hill, NJ uucp: {att,decvax,ucbvax}!ulysses!gsf internet: gsf@ulysses.att.com
chris@mimsy.UUCP (Chris Torek) (03/23/89)
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