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

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

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

Philadelphia Linux Users Group         --
Announcements -
General Discussion  --