[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: [Help-bash] How to tell if an environment variable exists or not
From: |
Bob Proulx |
Subject: |
Re: [Help-bash] How to tell if an environment variable exists or not |
Date: |
Tue, 6 Mar 2012 14:31:59 -0700 |
User-agent: |
Mutt/1.5.21 (2010-09-15) |
Stephane Chazelas wrote:
> Greg Wooledge wrote:
> > Bill Gradwohl wrote:
> > > Assuming set -u, how does one test to see if an environment
> > > variable exists without creating it in the process?
> >
> > if [[ ${foo+defined} ]]
> [...]
>
> Though that answer is probably what the OP was looking for,
> please note that it will not differenciate between environment
> variables and other shell variables.
>
> Also, this will only work for environment variables that can be
> mapped to shell variables and are not masqued by builtin shell
> variables (like *, 1...), some are being unexported or altered
> by bash anyway.
However programming a shell script with set -u in effect is
problematic on a number of levels. This is just one of them. I think
this is one of those features that sounds useful in theory and one
that many people would ask to have available but then in practice
turns out to be subtle and quick to anger. If it were a script I were
writing or maintaining I would remove the set -u and rewrite it to
work properly without it. In my experience that has been much less
trouble in the long run. YMMV.
Bob