Consider following code:
function child()
{
echo $var
}
function parent()
{
local var=5
child
}
I've tested it on my machine and it seems to work but I wasn't able to find anything definitive describing such usage of local variables. Namely, when I declare a local variable in one function and from that function I call some other function, can I use the variable in the latter (and even nest it deeper)? Is it legal in bash and is it standard for all versions?
When local is used within a function, it causes the variable name to have a visible scope restricted to that function and its children.The shell uses dynamic scoping, so that if the variable x is made local to function f, which then calls function g, references to the variable x made inside g will refer to the variable x declared inside f, not to the global variable named x.