#Bash 的 declare 命令
declare [OPTIONS] [NAME=VALUE]...
功能
定义变量(和函数),或设置变量的属性。
类型
Bash 内置命令。
参数
OPTION选项:-f- 只查看或操作函数-F- 仅显示函数名-g- 在 shell 函数中使用时创建全局变量-I- 如果创建局部变量,则继承先前作用域中同名变量的属性和值-p- 显示每个名称的属性和值-a- 使NAME具有“索引数组”属性-A- 使NAME具有“关联数组”属性-i- 使NAME具有“整数”属性-l- 使NAME在赋值时自动将值换为小写-n- 使NAME为变量的引用-r- 使NAME只读-t- 使NAME具有“trace”属性-u- 使NAME在赋值时自动将值换为大写-x- 导出(export)NAME+a- 移除NAME的“索引数组”属性+A- 移除NAME的“关联数组”属性+i- 移除NAME的“整数”属性+l- 移除NAME在赋值时自动转小写的属性+n- 移除NAME引用属性+r- 移除NAME的只读属性+t- 移除NAME的“trace”属性+u- 移除NAME在赋值时自动转大写的属性+x- 移除NAME的导出(export)属性(删除环境变量)
NAME- 变量名VALUE- 变量的值
#示例
查看变量
$ declare # 查看所有变量
BASH=/bin/bash
BASHOPTS=checkwinsize:cmdhist:complete_fullquote:expand_aliases:extglob:extquote:force_fignore:globasciiranges:globskipdots:histappend:interactive_comments:login_shell:patsub_replacement:progcomp:promptvars:sourcepath
BASH_ALIASES=()
BASH_ARGC=([0]="0")
BASH_ARGV=()
...
查看函数
$ declare -fF # 查看函数且只显示函数名
declare -f __expand_tilde_by_ref
declare -f __get_cword_at_cursor_by_ref
declare -f __git_eread
declare -f __git_ps1
declare -f __git_ps1_colorize_gitstring
...
定义引用变量
$ real_var="hello" # 定义普通变量
$ declare -n ref_var=real_var # 定义引用变量
$ echo $ref_var
hello
定义只读变量
$ declare -r PI=3.1415926 # 定义只读变量
$ echo $PI
3.1415926
$ PI=3 # 赋值会报错
-bash: PI: readonly variable
定义整数
$ declare -i num=10 # 定义整数
$ num+=5 # 整数可以进行运算
$ echo $num
15
自动转换大小写
$ declare -u upper # 自动转大小
$ upper=abc
$ echo $upper
ABC
$ declare -l lower # 自动转小写
$ lower=XYZ
$ echo $lower
xyz
定义数组
$ declare -a arr=(apple banana cherry) # 定义数组
$ echo "${arr[0]}" # apple
apple
$ echo "${arr[@]}" # 全部元素
apple banana cherry
定义关联数组(map/dict)
$ declare -A user # 定义关联数组
$ user[name]="Alice" # 元素赋值
$ user[age]=20
$ echo "${user[name]}"
Alice
$ echo "${user[age]}"
20
$ $ echo "${user[@]}" # 全部元素
20 Alice
环境变量
$ declare +x MY_ENV # 将 MY_ENV 从环境变量中移除
$ declare -x MY_ENV # 将 MY_ENV 加入环境变量
$ declare -x PATH="$PATH:/my/bin" # 相当于 export
#相关命令
| 命令 | 说明 |
|---|---|
| unset | 删除变量 |
#手册
declare: declare [-aAfFgiIlnrtux] [name[=value] ...] or declare -p [-aAfFilnrtux] [name ...]
Set variable values and attributes.
Declare variables and give them attributes. If no NAMEs are given,
display the attributes and values of all variables.
Options:
-f restrict action or display to function names and definitions
-F restrict display to function names only (plus line number and
source file when debugging)
-g create global variables when used in a shell function; otherwise
ignored
-I if creating a local variable, inherit the attributes and value
of a variable with the same name at a previous scope
-p display the attributes and value of each NAME
Options which set attributes:
-a to make NAMEs indexed arrays (if supported)
-A to make NAMEs associative arrays (if supported)
-i to make NAMEs have the `integer' attribute
-l to convert the value of each NAME to lower case on assignment
-n make NAME a reference to the variable named by its value
-r to make NAMEs readonly
-t to make NAMEs have the `trace' attribute
-u to convert the value of each NAME to upper case on assignment
-x to make NAMEs export
Using `+' instead of `-' turns off the given attribute.
Variables with the integer attribute have arithmetic evaluation (see
the `let' command) performed when the variable is assigned a value.
When used in a function, `declare' makes NAMEs local, as with the `local'
command. The `-g' option suppresses this behavior.
Exit Status:
Returns success unless an invalid option is supplied or a variable
assignment error occurs.