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

Test existence of shell variable, bash, csh



This came up on another list.  The problem involves testing existence of
a Unix shell variable from another program with limited system
interaction features.

I usually write this in bash as:

    if [ x${MYVAR} = x ]; then
        echo 'MYVAR doesn't exist (or isn't set)'
	else echo "MYVAR exists, value: $MYVAR"
    fi

...which essentially checks whether or not the variable has a non-null
value.  But would report that $MYVAR doesn't exist if in fact it was set
equal to "".

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?

-- 
Karsten M. Self <kmself@ix.netcom.com>    http://kmself.home.netcom.com/
 What part of "Gestalt" don't you understand?       There is no K5 cabal
  http://gestalt-system.sourceforge.net/         http://www.kuro5hin.org

Attachment: pgpK3qYBQAHH3.pgp
Description: PGP signature


Reply to: