bug-bash
[Top][All Lists]
Advanced

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

Re: initialisation bash variables


From: Stephane CHAZELAS
Subject: Re: initialisation bash variables
Date: Wed, 28 Mar 2012 18:25:57 -0000
User-agent: slrn/pre1.0.0-18 (Linux)

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).

> 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

-- 
Stephane


reply via email to

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