The problem with global variables
Suppose a script uses the variable i as a loop counter in the main body. Then it calls a helper function that also uses i as its own counter. The helper quietly overwrites the main counter, and the main loop ends early or runs forever. Nothing in the syntax warns you, because shell variables do not need a declaration. The name is simply shared by everything that runs in the same shell.
i=outer
helper() { i=inner; }
helper
echo "$i"The script prints inner. The function changed the one variable that the main code was also using. Functions are the main reason to scope variables carefully, because they are the place where you reuse names.
Declaring a local variable
The local builtin creates a variable that is visible only inside the function where it is declared, and inside any function called from there. When the function returns, the variable is removed and any global of the same name is visible again. You can give a local a value at the same time, and you can declare several at once with local a b c.
i=outer
helper() { local i=inner; echo "in helper: $i"; }
helper
echo "after: $i"The script prints in helper: inner and then after: outer. The local variable shadowed the global one during the call and left it untouched afterward. This is the form you should use for almost every variable inside a function, including loop counters and temporary strings.
Declare locals at the top of the function, before you assign anything else. A local without a value, as in local count, is created empty, so it does not inherit a global value of the same name.
Dynamic scope and what it means
Bash scopes locals dynamically, which means the visibility follows the call stack, not the text of the program. If outer declares a local x and then calls inner, then inner sees that x even though inner was defined somewhere else in the file. A lexically scoped language would not do that. Shell programmers rely on it deliberately, and sometimes it causes bugs when a helper reads a name it did not expect.
inner() { echo "inner sees x=$x"; }
outer() { local x=42; inner; }
outer
innerThe first call to inner prints inner sees x=42, because outer has a local x on the call stack. The final call prints inner sees x=, because no caller has declared x at that point. The helper did not change. Only the call stack around it did.
local is only valid inside a function
The local keyword is an error at the top level of a script or at the prompt. Bash reports that it can only be used in a function, and the command fails. This is a useful check: if you see that message, the declaration you meant to be inside a function has been placed outside it by mistake, often because a closing brace is missing.
local y=1
echo "status=$?"The first line prints an error message that names the local builtin, and the second line prints status=1. Running the same statement inside a function would succeed, which is why the error is informative rather than a mystery.
Locals, arrays, and declare
local is a synonym for a declare with a function-local scope, so it accepts the same attributes. local -a creates a local indexed array, and local -A creates a local associative array. The units on arrays use these forms. Use declare -p name to print the attributes and value of a variable, which is a good way to check what a function really created.
f() { local -a items=(one two); declare -p items; }
fThe output has the form declare -a items=([0]="one" [1]="two"). The exact spacing is set by bash, so do not depend on it in scripts. The attribute letters and the values are what matter.
Hands-on: prove the scope
Type the two functions below at the prompt, then call them. The point is to see that the global value comes back. The lab for this unit asks you to write the same test as a file, so that the result does not depend on the state of your interactive shell.
x=global
show() { echo "show: x=$x"; }
set_local() { local x=local; show; }
set_local
echo "after: x=$x"The output shows show: x=local and then after: x=global. The function show never declares x, so it reads whichever x is visible, and the caller decides what that is.