[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

Re: Test existence of shell variable, bash, csh



On Sat, May 05, 2001 at 02:34:47PM -0700, Karsten M. Self wrote:
> 
> In contrast, csh and derivatives have:
> 
>    $?MYVAR
> 
> ...which allows testing of presence of a variable.
> 
> ...but I'm not aware of a similar bash/korn/bourne feature.  Anyone?

Coming soon to ./configure scripts near you:

if test "${LANG+set}" = set; then LANG=C; export LANG; fi

or in your case:

if test "${MYVAR+set}" = set; then
    echo "MYVAR doesn't exist"
else
    echo "MYVAR exists, value: $MYVAR"
fi


also available in bash(1): 

       In each of the cases  below,  word  is  subject  to  tilde
       expansion,  parameter expansion, command substitution, and
       arithmetic  expansion.   When  not  performing   substring
       expansion,  bash  tests  for  a parameter that is unset or
       null; omitting the colon results in  a  test  only  for  a
       parameter that is unset.

       ${parameter:+word}
              Use Alternate  Value.   If  parameter  is  null  or
              unset, nothing is substituted, otherwise the expan­
              sion of word is substituted.
 

Since it is used in configure scripts, I'd guess it's pretty portable.


-- 
Tommi Komulainen                                 Tommi.Komulainen@iki.fi
GPG 1024D/68388EE6    6FD6 DD79 EB38 BF6F 3533  09C0 04A8 9871 6838 8EE6

Attachment: pgp9tUHkZbvav.pgp
Description: PGP signature


Reply to: