← 返回命令列表

Linux command

local 命令

趣味

复制后可按需替换文件名、目录或参数。

常用示例

Declare a local variable

local [varname]

Declare a local variable with an initial value

local [varname]="[value]"

Declare a local integer variable

local -i [count]=0

Declare a local indexed array

local -a [array]=([a] [b] [c])

Declare a local readonly variable

local -r [constant]="[value]"

Inherit attributes and value from a variable in the surrounding scope

local -I [varname]

说明

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.

参数

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

FAQ

What is the local command used for?

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.

How do I run a basic local example?

Run `local [varname]` in a terminal, then adjust file names, paths, flags, or remote targets for your system.

What does -a do in local?

Declare as indexed array.