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?
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?
The Bash Reference Manual is an authoritative source of information about bash.
Here's an example of testing a variable to see if it exists:
(From section 6.3.2.)
Note that the whitespace after the open
[
and before the]
is not optional.Tips for Vim users
I had a script that had several declarations as follows:
But I wanted them to defer to any existing values. So I re-wrote them to look like this:
I was able to automate this in vim using a quick regex:
This can be applied by selecting the relevant lines visually, then typing
:
. The command bar pre-populates with:'<,'>
. Paste the above command and hit enter.Tested on this version of Vim:
Windows users may want different line endings.
A summary of tests.
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:
You probably can combine the two tests on the second line into one with:
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).You can find the details of these, and other related shell parameter expansions, the
test
or[
command and conditional expressions in the Bash manual.I was recently asked by email about this answer with the question:
Fair question - the answer is 'No, your simpler alternative does not do the same thing'.
Suppose I write this before your test:
Your test will say "VAR is not set at all", but mine will say (by implication because it echoes nothing) "VAR is set but its value might be empty". Try this script:
The output is:
In the second pair of tests, the variable is set, but it is set to the empty value. This is the distinction that the
${VAR=value}
and${VAR:=value}
notations make. Ditto for${VAR-value}
and${VAR:-value}
, and${VAR+value}
and${VAR:+value}
, and so on.As Gili points out in his answer, if you run
bash
with theset -o nounset
option, then the basic answer above fails withunbound variable
. It is easily remedied:Or you could cancel the
set -o nounset
option withset +u
(set -u
being equivalent toset -o nounset
).-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:
Default value:
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.
The explicit way to check for a variable being defined would be:
Here is what I think is a much clearer way to check if a variable is defined:
Use it as follows: