local variables in bash
Shane Hathaway
shane at hathawaymix.org
Thu Feb 12 18:04:55 MST 2009
Merrill Oveson wrote:
> If you run this, you'll notice that loc_var prints as 23. It should print
> as 43.
> This is a problem with bash - that is if one function calls another the
> value of the local variables are seen.
It's a surprise, but it follows the bash documentation.
$ help local
local: local name[=value] ...
Create a local variable called NAME, and give it VALUE. LOCAL
can only be used within a function; it makes the variable NAME
have a visible scope restricted to that function and its children.
The "... and its children" specification is different from most
programming languages that I'm familiar with. I guess bash searches the
call stack for variables, rather than use scoping rules like most
languages. Another reason not to write anything complex in bash. :-)
Shane
More information about the PLUG
mailing list