Bash 5 reference · Shell Builtins and Syntax

local (Bash builtin)

Declare variables that exist only inside the current function, so they don't overwrite globals with the same name.

Bash 5

Create a free account to save this page, take notes and track your progress. Sign in

Syntax

local [-a|-A|-i|-r] NAME[=VALUE]...

Parameters

ParameterDescription
-a / -A Local indexed / associative array.
-r Read-only.

Examples

Local versus global
name=global
f() { local name=inside; echo "in f: $name"; }
f
echo "after f: $name"

Output

in f: inside
after f: global

Run this on your computer — see “Get Started” for setup.

An integer local
count() { local -i n=0; for _ in "$@"; do n+=1; done; echo "$n"; }
count a b c

Output

3

Run this on your computer — see “Get Started” for setup.

Was this page helpful?