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:59 -0000
User-agent: slrn/pre1.0.0-18 (Linux)

2011-08-17, 08:32(+02), Francky Leyn:
> 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?

Yes, unless you want to export them to the environment of
commands you start in that 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?

The environment is a list of strings (by convention of the format
var=value) passed upon executing a command in a fashion exactly
similar to the list of arguments to the command.

In other words, when you execute a command:

cmd arg1 arg2

you pass both a list of arguments ("cmd", "arg1", and "arg2")
and a list of environment variables ("PATH=/bin...",
"VAR=foo"...).

The difference being that the list of arguments is explicit on
the shell command line while the list of environment variables
comes from the remembered list of environment variables that the
shell (or any application that uses the C library and *environ,
putenv(3), setenv(3)) maintains internally (and initialised from
the environment it received when it was executed).

Though the shell allows the syntax:
VAR=value cmd arg1 arg2
to specify environment variables on the command line.

In no circumstance are variable definitions in one process going
to affect the environment of other processes (an exception to
that is the "fish" shell)

-- 
Stephane


reply via email to

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