Walt Mankowski on 13 Oct 2009 16:21:57 -0700 |
On Tue, Oct 13, 2009 at 03:52:24PM -0400, K.S. Bhaskar wrote: > 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. Well, I don't know if it's *better*, but if you've got perl installed on the box you could always say perl -e 'exit defined $ENV{foo}' which will set $? to either 1 or 0. But to be honest, instead of trying to come up with a solution to this problem, I'd probably concentrate on avoiding putting nulls in my environment variables in the first place. Walt Attachment:
signature.asc ___________________________________________________________________________ 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
|
|