[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 07:25:35PM -0700, Karsten M. Self wrote:
> on Sun, May 06, 2001 at 02:22:40AM +0300, Tommi Komulainen (Tommi.Komulainen@iki.fi) wrote:
> > 
> > if test "${LANG+set}" = set; then LANG=C; export LANG; fi
> 
> Close, but not quite.  It's probably the nearest thing to a winner I've
> seen posted though.  And you just edged out Alan's suggestion of an
> equivalent but opposite test.

Umm, you wanted to test if a variable is set, the test does exactly that,
not the opposite.


> The possible problem"  "*null or* unset".  An existing, but null,
> environment variable will be reported as unset.

hrmpf, I *know* I should've emphasized the following the first time:

> >        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.
           ^^^^^^^^^^^^^^^^^^^^^^^^


> It's possible to create a null environment variable, e.g.:
> 
>     $ foo=
>     $ echo $foo
> 
>     $ echo ${foo:-unset}
>     unset

Let's see...

$ foo=
$ echo $foo

$ echo ${foo-unset}

$ echo ${foo+set}
set
$ unset foo
$ echo ${foo-unset}
unset
$ echo ${foo+set}

$


RTFQ ;>


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

Attachment: pgp6QR3s4gMrF.pgp
Description: PGP signature


Reply to: