K.S. Bhaskar on 13 Oct 2009 12:52:30 -0700


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

[PLUG] Distinguishing between environment variable with null value and one that is not set


It appears that POSIX shell (and bash) have no built-in way to
distinguish between the case where an environment variable exists with
a null value and the case where it simply does not exist.  The
simplest way I have been able to come up with is to grep the output of
env and test for zero length, e.g.:

$ if [ -z "`env | grep foo`" ] ; then echo It doesn\'t exist\! ; fi
It doesn't exist!
$ if [ -z "`env | grep PWD`" ] ; then echo It doesn\'t exist\! ; fi
$

Is there a better way?  Thanks in advance.

Regards
-- Bhaskar
___________________________________________________________________________
Philadelphia Linux Users Group         --        http://www.phillylinux.org
Announcements - http://lists.phillylinux.org/mailman/listinfo/plug-announce
General Discussion  --   http://lists.phillylinux.org/mailman/listinfo/plug