I have a zsh function that looks like this:
outer_func () {
local LOCAL_VAR='test'
inner_func () {
echo "${LOCAL_VAR}"
}
}
where $LOCAL_VAR
is not available to inner_func
.
I know that I can use eval
to define inner_func
so that it can access the value of $LOCAL_VAR
, I wonder if there's another way to do it?
Edit: test on zsh 5.8:
$ cat test.zsh
outer_func () {
local LOCAL_VAR='test'
inner_func () {
echo "${LOCAL_VAR}"
}
}
echo 'executing outer_func'
outer_func
echo 'executing inner_func'
inner_func
$ ./test.zsh
executing outer_func
executing inner_func
$
Edit 2: Clarification: I actually use outer_func
to set variables to a series of inner_func
s which are essential for the inner_func
s to work, and after the definition of outer_func
, I need to call the inner_func
s.
LOCAL_VAR
is visible withininner_func
if I callinner_func
fromouter_func
inner_func
from withinouter_func
, like Andy said.inner_func
directly, not from withinouter_func
? (See Edit 2)inner_func
, it's going to use a variable namedLOCAL_VAR
that is in scope (if one exists). I thinkeval
is going to be your best bet.