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: Greg Wooledge
Subject: Re: Passing variables by reference conflicts with local
Date: Fri, 30 Apr 2010 08:19:54 -0400
User-agent: Mutt/1.4.2.3i

On Fri, Apr 30, 2010 at 12:02:58AM +0200, Freddy Vulto wrote:
> In an attempt to improve the bash-completion package we're trying to
> improve the bash completion library functions by passing variables by
> reference.

http://mywiki.wooledge.org/BashFAQ/006

> Passing variables by reference however, has a caveat in that
> local variables override the passing by reference, e.g.:
> 
>     t() {
>         local a
>         eval $1=b
>     }
>     unset a; t a; echo $a  # Outputs nothing, expected "b"

Why did you declare 'a' to be local if that's not what you wanted?
Why did you expect 'b' to be output, if you used a local variable?

> I'm thinking of some workarounds below?

All your workarounds still declare a local variable, which for some
reason you expect to be treated as non-local.  Stop doing that.

$ t() { eval $1=b; }; unset a; t a; echo "a is '$a'"
a is 'b'




reply via email to

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