how do I check for existance of env var in bash

  • Follow


How do I check if an environment variable exists and f it is defined in 
a bash shell?

-- 
    ___  _ ____       ___       __  __
   / _ )(_) / /_ __  / _ \___ _/ /_/ /____  ___
  / _  / / / / // / / ___/ _ `/ __/ __/ _ \/ _ \
/____/_/_/_/\_, / /_/   \_,_/\__/\__/\___/_//_/
            /___/
Texas Instruments ASIC Circuit Design Methodlogy Group
Dallas, Texas, 214-480-4455,  b-patton@ti.com

0
Reply Billy 3/4/2004 1:18:46 PM

"Billy N. Patton" <b-patton@ti.com> writes:

> How do I check if an environment variable exists and f it is defined
> in a bash shell?

if [ "$VAR" == "" ]
then echo VAR is undefined
else echo VAR is defined
fi

-- 
Maurizio Loreti                         http://www.pd.infn.it/~loreti/mlo.html
Dept. of Physics, Univ. of Padova, Italy              ROT13: ybergv@cq.vasa.vg
0
Reply Maurizio 3/4/2004 2:42:42 PM


In article <rmn06wsmfx.fsf@mlinux.pd.infn.it>, Maurizio Loreti <mlo@foobar.it> wrote:

>> How do I check if an environment variable exists and f it is defined
>> in a bash shell?
>
>if [ "$VAR" == "" ]
>then echo VAR is undefined
>else echo VAR is defined
>fi

VAR=
if [ "$VAR" == "" ]
then echo VAR is undefined
else echo VAR is defined
fi

--> VAR is undefined


What about the following?

set | grep "^VAR=" | wc -l

Gruesse aus der Schweiz
Salutations en provenance de Suisse
Saluti dalla Svizzera
Greetings from Switzerland
Happl
0
Reply Hanspeter 3/4/2004 3:28:50 PM

Billy N. Patton wrote:
> How do I check if an environment variable exists and f it is defined in 
> a bash shell?

 From the man page:

        In each of the cases  below,  word  is  subject  to  tilde
        expansion,  parameter expansion, command substitution, and
        arithmetic  expansion.   When  not  performing   substring
        expansion,  bash  tests  for  a parameter that is unset or
        null; omitting the colon results in  a  test  only  for  a
        parameter that is unset.

        ${parameter:-word}
               Use Default Values.  If parameter is unset or null,
               the expansion of word is  substituted.   Otherwise,
               the value of parameter is substituted.
        ${parameter:=word}
               Assign  Default  Values.   If parameter is unset or
               null, the expansion of word is assigned to  parame�
               ter.   The  value of parameter is then substituted.
               Positional parameters and  special  parameters  may
               not be assigned to in this way.
        ${parameter:?word}
               Display  Error  if  Null or Unset.  If parameter is
               null or unset, the expansion of word (or a  message
               to  that  effect if word is not present) is written
               to the standard error and the shell, if it  is  not
               interactive, exits.  Otherwise, the value of param�
               eter is substituted.
        ${parameter:+word}
               Use Alternate  Value.   If  parameter  is  null  or
               unset, nothing is substituted, otherwise the expan�
               sion of word is substituted.

0
Reply Jeff 3/4/2004 3:34:07 PM

in comp.unix.programmer i read:

>>> How do I check if an environment variable exists and f it is defined
>>> in a bash shell?

>What about the following?
>
>set | grep "^VAR=" | wc -l

ewww -- two external commands for a trivial test.  even compressed, where
the wc -l is eliminated and -c is added to the grep, is still much slower
than something simpler ...

  [ "${VAR+1}"  = 1 ] && echo VAR exists but may be empty
  [ "${VAR:+1}" = 1 ] && echo VAR exists and is not empty
  [ "${VAR:-1}" = 1 ] && echo VAR does not exist or is empty
  [ "${VAR-1}"  = 1 ] && echo VAR does not exist

-- 
a signature
0
Reply those 3/4/2004 8:10:01 PM

On Thu, 04 Mar 2004 at 20:10 GMT, those who know me have no need of my name wrote:
> in comp.unix.programmer i read:
> 
>>>> How do I check if an environment variable exists and f it is defined
>>>> in a bash shell?
> 
>>What about the following?
>>
>>set | grep "^VAR=" | wc -l
> 
> ewww -- two external commands for a trivial test.  even compressed, where
> the wc -l is eliminated and -c is added to the grep, is still much slower
> than something simpler ...
> 
>   [ "${VAR+1}"  = 1 ] && echo VAR exists but may be empty
>   [ "${VAR:+1}" = 1 ] && echo VAR exists and is not empty
>   [ "${VAR:-1}" = 1 ] && echo VAR does not exist or is empty
>   [ "${VAR-1}"  = 1 ] && echo VAR does not exist

    That's fine for a shell variable, but doesn't indicate whether the
    variable is an environment variable.

    The easiest way to do that is to use an external script:

$ cat qvar
## USAGE: qvar variable
QT='"'
for var
do
    eval  "
       if [ -z "\${$var+X}" ]
       then
          echo $var is not in the environment
       elif [ -n \"\${$var:-X}\" ]
       then
          echo \"$var is in the environment with the value of \$QT\$$var\$QT\"
       fi"
done
$ unset HISTSIZE
$ qvar HISTSIZE
HISTSIZE is not in the environment
$ HISTSIZE=5000
$ qvar HISTSIZE
HISTSIZE is not in the environment
$ export HISTSIZE
$ qvar HISTSIZE
HISTSIZE is in the environment with the value of "5000"

-- 
    Chris F.A. Johnson                  http://cfaj.freeshell.org/shell
    ===================================================================
    My code (if any) in this post is copyright 2004, Chris F.A. Johnson
    and may be copied under the terms of the GNU General Public License
0
Reply Chris 3/4/2004 8:33:09 PM

2004-03-4, 20:33(+00), Chris F.A. Johnson:
[...]
>     The easiest way to do that is to use an external script:
>
> $ cat qvar
> ## USAGE: qvar variable
> QT='"'
> for var
> do
>     eval  "
>        if [ -z "\${$var+X}" ]
>        then
>           echo $var is not in the environment
>        elif [ -n \"\${$var:-X}\" ]
>        then
>           echo \"$var is in the environment with the value of \$QT\$$var\$QT\"
>        fi"
> done
> $ unset HISTSIZE
> $ qvar HISTSIZE

But it doesn't work for vars like $-, $1, $BASH and environment
variable  whose name is not mappable to a shell variable.

With bash, you can use:

qvar() {
  eval set "$(declare -p "$1" 2> /dev/null)"
  [[ $# -gt 2 && $2 = *x* ]]
}

Otherwise, you can use:

qvar() {
  awk 'BEGIN {exit(!(ARGV[1] in ENVIRON))}' "$1"
}

-- 
St�phane                      ["Stephane.Chazelas" at "free.fr"]
0
Reply Stephane 3/5/2004 10:34:32 AM

> ewww -- two external commands for a trivial test.  even compressed, where
> the wc -l is eliminated and -c is added to the grep, is still much slower
> than something simpler ...
> 
>   [ "${VAR+1}"  = 1 ] && echo VAR exists but may be empty
>   [ "${VAR:+1}" = 1 ] && echo VAR exists and is not empty
>   [ "${VAR:-1}" = 1 ] && echo VAR does not exist or is empty
>   [ "${VAR-1}"  = 1 ] && echo VAR does not exist

Of course, this will fail if $VAR happens to be set to 1.

I suppose you could patch this up by doing:

  [ "${VAR-1}" = 1 -a "${VAR-2}" = 2 ] && echo VAR does not exist

However, bash's declare -p cmd (mentioned elsewhere) is almost
certainly the better way to go.

b
0
Reply blr 3/5/2004 11:56:05 PM

7 Replies
1420 Views

(page loaded in 0.085 seconds)

Similiar Articles:










7/22/2012 8:04:37 AM


Reply: