declare:声明变量的类型

declare [-aixr] var

参数:
-a:array
-i:int
-x:同export,声明为环境变量
+x:从环境变量声明为变量
-r:只读,无法重设

#变量的默认类型是 string
zmc@SUSE11:~> sum=1+2+3 
zmc@SUSE11:~> echo $sum
1+2+3
#声明为i
zmc@SUSE11:~> declare -i sum
zmc@SUSE11:~> sum=1+2+3
zmc@SUSE11:~> echo $sum
6
#设定为环境变量
zmc@SUSE11:~> declare -x sum 
zmc@SUSE11:~> export | grep sum
declare -ix sum="6"
#设定为只读
zmc@SUSE11:~> declare -r sum 
zmc@SUSE11:~> sum=$sum+4
bash: sum: readonly variable
#不可取消
zmc@SUSE11:~> declare +r sum 
bash: declare: sum: readonly variable

你可能感兴趣的:(declare:声明变量的类型)