It is currently Thu, 20 Jan 2022 09:44:14 GMT



 
Author Message
 Bash: How to detect existence of a variable?
Hello,

I'm trying to test for the existence of a variable, even if
it is a null variable.  Here is what I tried so far, based
on ideas from
http://www.**-**.com/ :

   ###   Uninitialized variable (not detected, of course)

   $PROMPT> unset blah
   $PROMPT> if [ $blah ] ; then echo dog ; fi
   $PROMPT> if [ "$blah" ] ; then echo dog ; fi
   $PROMPT> if [ -n "$blah" ] ; then echo dog ; fi

   ###   Variable set to zero-length string is *not* detected

   $PROMPT> blah=
   $PROMPT> if [ $blah ] ; then echo dog ; fi
   $PROMPT> if [ "$blah" ] ; then echo dog ; fi
   $PROMPT> if [ -n "$blah" ] ; then echo dog ; fi

   ###   But variable set to nonnull string is detected

   $PROMPT> blah=cat
   $PROMPT> if [ $blah ] ; then echo dog ; fi
      dog
   $PROMPT> if [ "$blah" ] ; then echo dog ; fi
      dog
   $PROMPT> if [ -n "$blah" ] ; then echo dog ; fi
      dog

   ###   Try to use "declare" to detect null-string variable

   unset blah

   ###   This works, but I get an ugly error
   $PROMPT> if declare -p blah ; then echo dog ; fi
      bash: declare: blah: not found

   ###   This doesn't work because "declare" ends up defining
   ###   the variable "blah" even if it didn't exist before
   $PROMPT> if declare blah ; then echo dog ; fi
      dog

   ###   This works, but is clumsy
   unset blah
   $PROMPT> if declare -p blah >/dev/null 2>&1  ; then echo dog ; fi
   $PROMPT> blah=
   $PROMPT> if declare -p blah >/dev/null 2>&1  ; then echo dog ; fi
      dog

Surely there must be a more direct way?  In csh, I can just do $?blah.

Fred
--
Fred Ma
Dept. of Electronics, Carleton University
1125 Colonel By Drive, Ottawa, Ontario
Canada, K1S 5B6



 Fri, 08 Sep 2006 09:39:42 GMT   
 Bash: How to detect existence of a variable?
On 22 Mar 2004 01:39:42 GMT, Fred Ma <f...@doe.carleton.ca> wrote:

[ "${foo+xxx}" ] && echo is set || echo is not set

Cheers,
--
Why is there only one Monopolies Commision?



 Fri, 08 Sep 2006 12:21:25 GMT   
 Bash: How to detect existence of a variable?

    This is never correct.

    This will have problems with certain values of $blah.

    This is the way to determine whether or not the variable has any
    contents, regardless of whether it is set or not.

[snip]

    To test whether the variable is set, use:

[ -n "${var+V}" ]

--
    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



 Fri, 08 Sep 2006 13:31:13 GMT   
 Bash: How to detect existence of a variable?

You're right. there's indeed a more direct way:

case ${blah+XXX} in
   ""):;;                        # var blah is unset here
    *) echo "'${blah}' is SET";; # var blah is set here, can be NULL
esac



 Fri, 08 Sep 2006 16:22:21 GMT   
 Bash: How to detect existence of a variable?

Neil, Chris, Rakesh,

Thanks for your examples.  The natural question that came up
is why I didn't find this syntax, and what it exactly does.
I looked again in the man page and the info page.  I was
poking mostly around the EXPANSION section.  I'm unable to
find the description of the syntax

   ${ Parameter_Name + Some_Other_Letters },

The closest is ${ParamName:+ConditionalReplacementText}.  I
was wondering if someone could kindly point out the place I
am overlooking.  Thanks.

Fred
--
Fred Ma
Dept. of Electronics, Carleton University
1125 Colonel By Drive, Ottawa, Ontario
Canada, K1S 5B6



 Sat, 09 Sep 2006 05:22:26 GMT   
 Bash: How to detect existence of a variable?

   That IS the section.

   A little higher up in the man page:

       In each of the cases below, word is subject to tilde expansion, parame-
       ter expansion, command substitution, and  arithmetic  expansion.   When
       not  performing substring expansion, bash tests for a parameter that is

--
    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



 Sat, 09 Sep 2006 05:55:53 GMT   
 Bash: How to detect existence of a variable?

Thanks again, Chris.  Sometimes I can be "effectively" dyslectic,
but I think this is just a case of getting the know the ropes of
bash so that I know what I'm looking for in the help pages.  It's
all there.

Fred
--
Fred Ma
Dept. of Electronics, Carleton University
1125 Colonel By Drive, Ottawa, Ontario
Canada, K1S 5B6



 Sat, 09 Sep 2006 14:18:21 GMT   
 
   [ 7 post ] 

Similar Threads

1. BASH BASH BASH BASH BASH BASH BASH BASH BASH BASH

2. Test for the existence of a variable

3. sh: testing for the existence of a variable?

4. Existence of Env Variable in ksh

5. ? How to check existence of variable name passed as argument

6. detecting difference between unset variable and variable whose value is null in Korn Shell

7. Bash referencing variables from other variables

8. Bash: referencing variables from other variables

9. BASH $variable to AWK $variable in script?

10. Setting variables with variables (sh/bash) [Summary]


 
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group.
Designed by ST Software