On 8/15/2011 9:24 PM, Stephane CHAZELAS wrote:
2011-08-15, 17:15(+02), Francky Leyn:
Hello,

if you have a variable, say VAR,
and you don't assign it a value,
and afterwards you test it,
what is the value of $VAR then?

random, or an empty string?
[...]

Upon startup, the shell makes one shell variable per environment
variable whose name is compatible with shell variable names.

So for instance, if bash receives "VAR=foo" in its environemt,
$VAR will expand to foo. If it's passed "1=bar", $1 will not be
affected, and it's the same for a few special variables of the
shell.

If passed "A+B=C" or "=D" for instance, that obviously won't be
mapped to shell variables. Some shells do discard variables from
the environment that can't be mapped to shell variables. That's
not the case of bash.

If you don't initialise a sh/bash variable, and ask it up
afterwards, which value does it have then? Is that random
or the empty string?

At the moment I'm doing the following:

VAR=FALSE
# some command line procesing, that can set VAR to "TRUE"
if [ $VAR = TRUE ]; then
...
fi

Must I effectively write that VAR=FALSE?
Or will the script work fine without?

Also, can't I write the test as

if [ $VAR ]; then
...
fi

Regards,

Francky






Reply via email to