declare命令用于声明 shell 变量,declare为shell指令。
1. 语法
declare [+/-][rxi][变量名=值]
可用来声明变量并设置变量的属性([rix]即为变量的属性)
[root@node1 ~]# help declare
declare: declare [-aAfFgilrtux] [-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
-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 an error occurs.
2. 参数
+/- "-“可用来指定变量的属性,”+"则是取消变量所设的属性。可以累加。
-f 仅显示函数。
r 将变量设置为只读。
x 指定的变量会成为环境变量,可供shell以外的程序来使用。
i [设置值]可以是数值,字符串或运算式。
3. 示例
3.1 显示全部的shell变量与函数
[root@node1 ~]# declare -f
3.2 显示指定的函数
[root@node1 ~]# declare -f quote
quote ()
{
local quoted=${1//\'/\'\\\'\'};
printf "'%s'" "$quoted"
}
3.3 声明整数型变量
[root@node1 ~]# declare -i a
[root@node1 ~]# declare -p a
declare -ail a='([0]="0")'
3.4 改变变量属性
[root@node1 ~]# declare -i a
[root@node1 ~]# a=1
[root@node1 ~]# echo $a
1
[root@node1 ~]# a="str"
[root@node1 ~]# echo $a
0
[root@node1 ~]# declare +i a
[root@node1 ~]# a="str"
[root@node1 ~]# echo $a
str
3.5 转换大小写
[root@node1 ~]# declare -i
[root@node1 ~]# declare -l s
[root@node1 ~]# s="A"
[root@node1 ~]# echo $s
a
3.6 设置变量为只读
[root@node1 ~]# declare -r a
[root@node1 ~]# a=12
-bash: a: readonly variable
3.7 定义数组变量
[root@node1 ~]# declare -a cd='([0]="a" [1]="b" [2]="c")'
[root@node1 ~]# echo ${cd[1]}
b
[root@node1 ~]# echo ${cd[@]}
a b c
[root@node1 ~]# declare -a a[3]
[root@node1 ~]# a[0]=1
-bash: a: readonly variable
3.8 允许算术计算
[root@node1 ~]# n=6/3
[root@node1 ~]# echo $(($n))
2
[root@node1 ~]# echo $[ $n ]
2
[root@node1 ~]# echo $n
6/3
[root@node1 ~]# declare -i n
[root@node1 ~]# echo $n
6/3
[root@node1 ~]# n=6/3
[root@node1 ~]# echo $n
2
[root@node1 ~]# n=6/4
[root@node1 ~]# echo $n
1
3.9 输出环境变量
declare -x var
export var
#declare命令允许在声明变量类型的时候同时给变量赋值。
declare -x var3=373
3.10 综合案例
#!/bin/bash
func1 ()
{
echo This is a function.
}
declare -f # 列出上面的函数.
echo
declare -i var1 # var1是一个整数.
var1=365
echo "var1 declared as $var1"
var1=var1+1 # 整数声明后,不需要使用'let'.
echo "var1 incremented by 1 is $var1."
#试图将已声明为整数的变量的值更改为浮点值.
echo "Attempting to change var1 to floating point value, 2367.1."
var1=2367.1 # 引起一个错误信息,此变量的值保持原样.
echo "var1 is still $var1"
echo
declare -r var2=13.36 # 'declare'允许设置变量的属性,
echo "var2 declared as $var2" # 试图更改只读变量的值.
var2=13.37 # 引起错误,并且从脚本退出.
echo "var2 is still $var2" # 这行不会被执行.
exit 0 # 脚本不会从这儿退出.
3.11 用declare声明局部变量
不加declare的FOO是全局变量
foo ()
{
FOO="bar"
}
bar ()
{
foo
echo $FOO
}
# 打印 bar.
bar
加declare的FOO是局部变量
foo ()
{
declare FOO="bar"
}
bar ()
{
foo
echo $FOO
}
#什么也不打印.
bar
————Blueicex 2020/03/03 23:51 blueice1980@126.com