|
[Date Prev] [Date Next] [Thread Prev] [Thread Next] [Date Index] [Thread Index]
|
Re: [PLUG] Distinguishing between environment variable with null value and one that is not set
|
On Tue, 13 Oct 2009, 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.:
[...]
> Is there a better way? Thanks in advance.
% typeset -p foo
typeset: no such variable: foo
% echo $?
1
% foo=''
% typeset -p foo
typeset foo=''
% echo $?
0
% if typeset -p foo 1>&2; then echo yup; else echo nope; fi 2> /dev/null
yup
% unset foo
% if typeset -p foo 1>&2; then echo yup; else echo nope; fi 2> /dev/null
nope
Doing a more normal '2>&1 >/dev/null' immediately after the typeset doesn't
work (I'm sure there's a good reason, but I'd have to figure it out), so we
hack it by redirecting stdout for the typeset (1) to stderr (2) and then
redirecting stderr for the whole compound command to /dev/null.
Paul
___________________________________________________________________________
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
|
|