On 5/3/10 3:30 PM, Freddy Vulto wrote:
> On 100503 08:57, Chet Ramey wrote:
>>> On Sat, May 01, 2010 at 04:26:16AM -0500, Dennis Williamson wrote:
>>>> I prefer to avoid using eval by using declare, but declare inside a
>>>> function makes the variable local. Wouldn't it be nice to have a
>>>> global flag (declare -g) like zsh's typeset -g.
>
>> I am planning a feature like this for a future release. It may end up
>> in bash-4.2.
>
> What I'm happily exploiting in this thread (and trying to get consent
> for) is the fact that declaring a global within a function does NOT
> automatically make the variable global, but instead allows one to pass
> variables between functions. In this light I also find the term
> "global" misleading.
Think of a chain of variable scopes corresponding to a chain of function
calls. When you look for a variable, you proceed "up" the scope
chain to the "root", returning the first instance you find. The requested
`declare -g' feature would create a variable at the global scope, rather
than the scope corresponding to the current function.
> This thread is still leaving me with the feeling I'm doing something
> wrong. Is this a documented and maintained bash feature? Can we safely
> apply this feature to the bash-completion package? Will a `declare -g'
> preserve the existing behaviour in say bash-4.2?
Unless you use it, `declare -g' won't matter. If you use it, the
proposed `declare -g' will declare variables at the global scope, as
if the declare had not been supplied.
> In other words: is it safe to exploit the behaviour below - 'a=A b=B'
> not becoming global?
That doesn't have anything to do with any proposed -g option to declare,
since you are not and won't be using it. Your code declares local
variables at one function scope and accesses them in a called function.
Bash variable scoping is dynamic. It will continue to work this way.
Chet
--
``The lyf so short, the craft so long to lerne.'' - Chaucer
``Ars longa, vita brevis'' - Hippocrates
Chet Ramey, ITS, CWRU [email protected] http://cnswww.cns.cwru.edu/~chet/