rostamia@umbc3.UMBC.EDU (Rouben Rostamian) (03/21/89)
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. Sorry if the question is too elementary; I am new to unix. -- Rouben Rostamian Department of Mathematics e-mail: University of Maryland Baltimore Counnty Rostamian@umbc2.bitnet Baltimore, MD 21228 rostamia@umbc3.umbc.edu
gwyn@smoke.BRL.MIL (Doug Gwyn ) (03/22/89)
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. >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.
maart@cs.vu.nl (Maarten Litmaath) (03/22/89)
rostamia@umbc3.UMBC.EDU (Rouben Rostamian) writes:
\... how can I tell, in Bourne Shell, whether the variable "foo" exits.
[ ${foo+RTFM} ]
--
Modeless editors and strong typing: |Maarten Litmaath @ VU Amsterdam:
both for people with weak memories. |maart@cs.vu.nl, mcvax!botter!maart
jay@hqda-ai.UUCP (Jay Heiser) (03/28/89)
Take a look at 'test' (type 'man test' for info). [ -z "$foo" ] is true if the length of $foo is zero [ -n "$foo" ] is true if the length of $foo is non-zero I've found this to be more reliable than [ $foo ] or X"$foo" = X Jay G. Heiser
davidsen@steinmetz.ge.com (Wm. E. Davidsen Jr) (03/29/89)
I've been following this, and I'm not sure the right answer has come out. The construct is ${foo+X} okay, but I think you want [ -z "${foo+X}" ] && echo "foo set" || echo "foo not set" construction, because without the -z some funny things seem to happen on older (V7/Sys III) shells. Just a thought. -- bill davidsen (wedu@crd.GE.COM) {uunet | philabs}!steinmetz!crdos1!davidsen "Stupidity, like virtue, is its own reward" -me