LinuxCommandLibrary
GitHubF-DroidGoogle Play Store

local

bash builtin that declares variables with local scope within a function

TLDR

Declare a local variable
$ local [varname]
copy
Declare a local variable with an initial value
$ local [varname]="[value]"
copy
Declare a local integer variable
$ local -i [count]=0
copy
Declare a local indexed array
$ local -a [array]=([a] [b] [c])
copy
Declare a local readonly variable
$ local -r [constant]="[value]"
copy
Inherit attributes and value from a variable in the surrounding scope
$ local -I [varname]
copy

SYNOPSIS

local [option] name[=value] ...

DESCRIPTION

local is a bash builtin that declares variables with local scope within a function. Local variables are not visible outside the function where they are declared.Using local prevents function variables from polluting or conflicting with the global namespace. The return status is zero unless local is used outside a function, an invalid name is supplied, or the variable is readonly.

PARAMETERS

-a

Declare as indexed array.
-A
Declare as associative array.
-i
Declare as integer.
-l
Convert to lowercase.
-u
Convert to uppercase.
-r
Make readonly.
-n
Name reference to another variable.
-x
Mark for export to child processes.
-I
Inherit attributes and value from a variable with the same name at a surrounding scope.

EXAMPLE

$ my_function() {
    local result="success"
    local -i count=5
    echo "$result: $count"
}
# $result and $count not accessible here
copy

CAVEATS

Only valid inside functions. Not POSIX-compliant (use typeset for portability). Local variables shadow global ones. Subshells inherit but cannot modify.

SEE ALSO

declare(1), typeset(1), export(1), readonly(1), unset(1), bash(1)

Copied to clipboard
Kai