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
Syntax
local [-a|-A|-i|-r] NAME[=VALUE]...
Parameters
| Parameter | Description |
|---|---|
-a / -A |
Local indexed / associative array. |
-r |
Read-only. |
Examples
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.
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.
Related
function· Group commands into a named function that takes arguments like a script ($1, $@) and returns an exit status.declare· Declare variables with attributes: arrays, associative arrays, integers, read-only values and exported variables.- Bash Functions · tutorial