sean finney on 13 Oct 2009 13:52:35 -0700


[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


hi,

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

this is POSIXly correct afaik:

	sh -u -c 'echo $foo' >/dev/null 2>&1

and has the added benefit of only a single subshell call, putting the result
in the test(1)'able $?.  if you want to get fancy and generalize:

isset(){
  local varname
  varname=$1
  eval 'sh -u -c "echo \$$varname"' >/dev/null 2>&1
}


hth,
	sean

Attachment: signature.asc
Description: Digital signature

___________________________________________________________________________
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