I'm trying to define a zsh function in a batch, using a for loop. I would expect to get different functions, e.g. pre1post
, pre2post
etc., each of which referring to the value of the variable at the moment of function creation.
From what I tried, I get the name of the function correctly but the value used in the loop refers to the last value in the array (as if it was a global variable?).
How can I save the current value of the variable in each defined function?
DESCRIPTORS=("1" "2" "3")
for i in $DESCRIPTORS; do
function "pre"$i"post"() {
echo i=$i
local j=$i
echo j=$j
eval "k=$i; echo k=$k"
echo p=${(P)ENV_DESCR}
eval "value=\"\${$ENV_DESCR}\""
echo v="$value"
}
done;
> pre1post
i=3
j=3
k=
p=
v=