变量声明declare,简单运算符运算,变量测试与内容替换
declare -/+ 选项 变量名
- 设类型
+ 取消类型
-i 设为整型
-x 设为环境变量
-p 显示类型属性(property)
[root@localhost ~]# a=1 [root@localhost ~]# declare -p a declare -- a="1" [root@localhost ~]# export a [root@localhost ~]# declare -p a declare -x a="1" [root@localhost ~]# declare -i a [root@localhost ~]# declare -p a declare -ix a="1" [root@localhost ~]# b=2 [root@localhost ~]# c=$a+$b [root@localhost ~]# echo $c 1+2 [root@localhost ~]# declare -i d=$a+$b [root@localhost ~]# echo $d 3 [root@localhost ~]# e=$(expr $a + $b) [root@localhost ~]# echo $e 3 [root@localhost ~]# f=$(($a+$b)) [root@localhost ~]# echo $f 3 [root@localhost ~]# g=$(expr $a+$b) [root@localhost ~]# echo $g 1+2 [root@localhost ~]# h=$[$a+$b] [root@localhost ~]# echo $h 3
变量测试与内容替换
[root@localhost ~]# x=${y-new} [root@localhost ~]# echo $x new [root@localhost ~]# y="" [root@localhost ~]# x=${y-new} [root@localhost ~]# echo $x [root@localhost ~]# y=0 [root@localhost ~]# x=${y-new} [root@localhost ~]# echo $x 0