declare: declare [-aAfFgilnrtux] [-p] [name[=value] ...]
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
-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 NAMEs 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 NAMEs 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.
declare命令用于声明和显示已存在的shell变量。declare命令若不带任何参数选项,则会显示所有shell变量及其值。declare的功能与typeset命令的功能是相同的。
查到与typeset命令的功能相同,所以也找了一下typeset的相关内容,发现:
typeset: typeset [-aAfFgilrtux] [-p] 名称[=值] ...
设置变量的值和属性。
废弃。参见 `help declare'。
typeset这个命令已经被废弃所以我们就专心的了解declare这个命令就好了!
显示:
-f:显示函数和定义;
-F 仅显示函数的名字(当调试的时候,加上行数和源文件);
-g 用于在shell中的函数建立全局的变量
-p 显示每一个名字的属性和值
有关于属性的参数:
+/-:"-"可用来指定变量的属性,"+"则是取消变量所设的属性
r:将变量设置为只读;
x:指定的变量会成为环境变量,可供shell以外的程序来使用;
i:让名字具有整数的性质
其他参数详见上面的help信息。
求算术运算式的值可以参见"let命令"!
在一个函数中,declare使一个名字变得的局部,这个地方与local命令的功能相同,但是-g可以抑制这个行为。