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: Pierre Gaston
Subject: Re: Passing variables by reference conflicts with local
Date: Sat, 1 May 2010 12:40:54 +0300

On Sat, May 1, 2010 at 12:26 PM, Dennis Williamson
<dennistwilliamson@gmail.com> wrote:
> As Chet said, use internal variables that are unlikely to conflict.
>
> # Param: $1  variable name to return value to
>   blackbox() {
>       local __bb_a    # internal: __, blackbox: bb, a: _a
>       eval $1=bar
>   }
>
> f() {
>       local a
>       blackbox a
>       echo $a
>   }
>   f    # no conflict
>
> 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.
>
> d='ls /;true'   # aren't you glad it's not "rm -rf" instead of "ls"
> f "$d"  # oops

You can use workarounds like:
printf -v $a "%s" foobar
read $a <<< "%s"




reply via email to

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