$_ CLI In Depth
Learning progress 0%
lesson functions arrays parameter-expansion

local Variables & Scope

4 min read Module 14: Functions, Arrays & Parameter Expansion Updated Dec 5, 2026
Cheat Sheet
0:00 / 3:08

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.

BASH4 lines
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.

BASH4 lines
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.

Tip

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.

BASH4 lines
inner() { echo "inner sees x=$x"; }
outer() { local x=42; inner; }
outer
inner

The 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.

BASH2 lines
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.

BASH2 lines
f() { local -a items=(one two); declare -p items; }
f

The 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.

BASH5 lines
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.

Terminal lab · 2 steps · real Linux in your browser

Prove the scope of local

Write a script that shows a local variable shadowing a global one, then capture a real error from local at top level.

01
Write the scope script
Create scope.sh in /workspace. It sets a global x, defines show to print x, and defines set_local to declare a local x and call show. The last line prints the global value again: ``bash cat > scope.sh <<'EOF' x=global show() { echo "show: x=$x"; } set_local() { local x=local; show; } set_local echo "after: x=$x" EOF `` Run bash scope.sh and confirm the two lines match what you expect.
Show hint for step 1
The line inside set_local must use the word local, so the function's own x hides the global one while show runs.
02
Capture the error from local
Run local at the top level of a bash -c command and save its error message to err.txt in /workspace. The message comes from bash, so keep the stderr redirection: ``bash bash -c 'local y=1' 2> err.txt `` Open err.txt and find the words can only be used in a function.
Show hint for step 2
Redirect standard error with 2> err.txt. Standard output is empty here, so you only need the error stream.
Check yourself Question 1 of 4
Concept Check Default scope of an assignment recall

Inside a function, an assignment such as count=5 without local creates a variable with what scope?

Recap & Key Takeaways

Shell variables are global by default, so a function that assigns a name without local can overwrite a variable used by the caller.
local creates a variable visible inside the function and any function it calls, and it is removed when the function returns.
Bash uses dynamic scope, so a called function sees the locals of its callers, which is useful but must be understood to avoid surprises.
local is an error outside a function, and declare -p name shows the attributes and value of a variable.
Was this lesson helpful? Spot an issue or have a suggestion? Let us know on GitHub.
Report an issue