Paul L. Snyder on 13 Oct 2009 14:21:47 -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


On Tue, 13 Oct 2009, sean finney wrote:

> On Tue, Oct 13, 2009 at 04:45:45PM -0400, Paul L. Snyder wrote:
> >   % typeset -p foo
> 
> just fyi, that's a bashism (or at least, it's not POSIX), so you can
> likely run into trouble if you ship it with a #!/bin/sh shebang.  for
> example, it's not in dash, which is the standard /bin/sh for debian and
> ubuntu these days.

Good point, I should have specified that (though Bhaskar did mention bash
in his message).

> > 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
> 
> i think you want >/dev/null 2>&1 (the order is significant).

Ah, quite right, well-spotted.

How about this, which should be POSIX-compliant (and doesn't require a
subshell):

  % foo=bar
  % if [[ -z ${foo+isset} ]]; then echo nope; else echo yep; fi
  yep
  % foo=''
  % if [[ -z ${foo+isset} ]]; then echo nope; else echo yep; fi
  yep
  % unset foo
  % if [[ -z ${foo+isset} ]]; then echo nope; else echo yep; fi
  nope

'isset' doesn't matter, it could be any non-zero-length string.

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