Recently while working I noticed this behavior of local variables. I wrote this piece of code to simplify my doubt.
foo () {
echo "This is moo : $MOO_VAR"
}
moo () {
local MOO_VAR="Hi MOO!"
foo
}
echo "calling moo."
moo
I can't understand how function foo can access the value of local variable MOO_VARof function moo, although foo cannot change the value of MOO_VAR . I can't find any explanation for this behavior. Can somebody explain why this happens ?