Zsh Mailing List Archive
Messages sorted by: Reverse Date, Date, Thread, Author

Re: new user questions and issues

Well, ok, that is interesting.

The reason to declare local is to be tight and explicit about variable scope.  I have been bitten with other shells in which variables are visible far outside where I would expect, leading to unexpected side effects.  The most egregious is with loop control variables for me.

What I really desire is that the variable not be inherited by or visible to any called functions.


> On May 10, 2015, at 10:02 PM, Bart Schaefer <schaefer@xxxxxxxxxxxxxxxx> wrote:
> Most of this has been answered already, throwing one extra remark.
> On May 6, 10:37am, Kannan Varadhan wrote:
> }
> } Issue #2.  Overridden local variables get echoed?
> The tidbit that no one has explicitly mentioned is that "local" et al.
> has function scope, so it makes no sense to declare something "local"
> inside a loop body.  (In an if/else, it could make sense, as long as
> the if/else is not itself inside a loop, but the variables declared
> there will persist beyond the "fi".)
> If you for some reason need a declaration to be local to a loop, use
> an anonymous function context like this:
>    local _t2
>    for _t2 in 0 1 2 ; do
>      function {
>        local _t3
>        _t3=XXX__${_t2}__
>      }
>    done

Messages sorted by: Reverse Date, Date, Thread, Author