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:58 -0000
User-agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:5.0) Gecko/20110624 Thunderbird/5.0

On 8/16/2011 10:53 PM, Stephane CHAZELAS wrote:
2011-08-16, 22:24(+02), Francky Leyn:
[...]
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?

Yes, you must write it, because bash may inherit a VAR variable
from the environment like I said (especially when you consider
that all uppercase variables are by convention reserved for
environment variables).

1) So it's a bad idea to use uppercase variables in a script?
2) If VAR coincides with an environment variable, and in the
   script I change it value, is this then propagated to outside
   the script? Is the environment variable affected?

Also, can't I write the test as

if [ $VAR ]; then
...
fi
[...]

No. That syntax is wrong.

Valid syntaxes are:

if [ "$VAR" != "" ]

if [ -n "$VAR" ]

if [ "$VAR" ]

Or if you want to be extremely portable:

if [ "" != "$VAR" ]
or
if [ "x$VAR" != x ]

Personally, I prefer:

var=false
if ... var=true ...

if "$var"; then
   ...
fi




reply via email to

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