views:

6887

answers:

4

If I want to check for the null string I would do

[ -z $mystr ]

but what if I want to check whether the variable has been defined at all? Or is there no distinction in bash scripting?

+4  A: 
~> if [ -z $FOO ]; then echo "EMPTY"; fi
EMPTY
~> FOO=""
~> if [ -z $FOO ]; then echo "EMPTY"; fi
EMPTY
~> FOO="a"
~> if [ -z $FOO ]; then echo "EMPTY"; fi
~>

-z works for undefined variables too. To distinguish between an undefined and a defined you'd use the things listed here or, with clearer explanations, here.

Cleanest way is using expansion like in these examples. To get all your options check the Parameter Expansion section of the manual.

Alternate word:

~$ unset FOO
~$ if test ${FOO+defined}; then echo "DEFINED"; fi
~$ FOO=""
~$ if test ${FOO+defined}; then echo "DEFINED"; fi
DEFINED

Default value:

~$ FOO=""
~$ if test "${FOO-default value}" ; then echo "UNDEFINED"; fi
~$ unset FOO
~$ if test "${FOO-default value}" ; then echo "UNDEFINED"; fi
UNDEFINED

Of course you'd use one of these differently, putting the value you want instead of 'default value' and using the expansion directly, if appropriate.

Vinko Vrsalovic
But I want to distinguish between whether the string is "" or hasn't been defined ever. Is that possible?
SetJmp
this answer *does* tell how to distinguish between those two cases; follow the bash faq link it provides for more discussion.
Charles Duffy
I added that after his comment, Charles
Vinko Vrsalovic
Look up "Parameter Expansion" in the bash man page for all these "tricks". E.g. ${foo:-default} to use a default value, ${foo:=default} to assign the default value, ${foo:?error message} to display an error message if foo is unset, etc.
Jouni K. Seppänen
A: 

call set without any arguments.. it outputs all the vars defined..
the last ones on the list would be the ones defined in your script..
so you could pipe its output to something that could figure out what things are defined and whats not

adi92
I didn't down-vote this, but it is something of a sledgehammer to crack a rather small nut.
Jonathan Leffler
A: 

I would think there is no distinction in bash scripting between a variable being null and not existing, given that "$VAR" returns the same result for both cases. In this case, the [ -z ] test should be adequate.

staticsan
+4  A: 

I think the answer you are after is implied (if not stated) by Vinko's answer, though it is not spelled out simply. To distinguish whether VAR is set but empty or not set, you can use:

if [ -z "${VAR+xxx}" ]; then echo VAR is not set at all; fi
if [ -z "$VAR" ] && [ "${VAR+xxx}" = "xxx" ]; then echo VAR is set but empty; fi

You probably can combine the two tests on the second line into one with:

if [ -z "$VAR" -a "${VAR+xxx}" = "xxx" ]; then echo VAR is set but empty; fi

However, if you read the documentation for Autoconf, you'll find that they do not recommend combining terms with '-a' and do recommend using separate simple tests combined with &&. I've not encountered a system where there is a problem; that doesn't mean they didn't used to exist (but they are probably extremely rare these days, even if they weren't as rare in the distant past).

Jonathan Leffler