bug-bash
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: initialisation bash variables


From: Francky Leyn
Subject: Re: initialisation bash variables
Date: Wed, 28 Mar 2012 18:25:56 -0000
User-agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:5.0) Gecko/20110624 Thunderbird/5.0

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

[Prev in Thread] Current Thread [Next in Thread]