bug-bash
[Top][All Lists]
Advanced

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

Re: Passing variables by reference conflicts with local


From: Freddy Vulto
Subject: Re: Passing variables by reference conflicts with local
Date: Mon, 3 May 2010 21:30:38 +0200
User-agent: Mutt/1.5.18 (2008-05-17)

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.

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?

In other words: is it safe to exploit the behaviour below - 'a=A b=B'
not becoming global?

    a=1; unset b
    t() {
        a=A b=B
    }
    u() {
        local a b
        t
        echo $a $b
    }
    u           # Outputs "A B"
    echo $a $b  # Outputs "1  ": globals aren't touched nor created


Freddy Vulto
http://fvue.nl/wiki/Bash:_passing_variables_by_reference




reply via email to

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