shell 基础5

数值运算:

declare:声明变量类型

declare [+/-][选项] 变量名
选项:
-:给变量设定类型属性
+:取消变量的类型属性
-i:将变量声明为整数型(integer)
-x:将变量声明为环境变量
-p:显示指定变量的被声明的类型

[root@localhost tmp]# aa=11
[root@localhost tmp]# bb=22
[root@localhost tmp]# cc=$aa+$bb
[root@localhost tmp]# echo $cc
11+22

方法一:
[root@localhost tmp]# declare -i cc=$aa+$bb
[root@localhost tmp]# echo $cc
33

方法二:
[root@localhost tmp]# aa=11
[root@localhost tmp]# bb=22
[root@localhost tmp]# dd=$(expr $aa + $bb)
[root@localhost tmp]# echo $dd
33
说明:+号两侧的空格不能省略

方法三:
[root@localhost tmp]# aa=11
[root@localhost tmp]# bb=22
[root@localhost tmp]# ee=$(($aa + $bb))
[root@localhost tmp]# echo $ee
33


[root@localhost tmp]#
[root@localhost tmp]# declare -p cc
declare -i cc="33"
[root@localhost tmp]# declare -p aa
declare -- aa="11"
[root@localhost tmp]# declare -p bb
declare -- bb="22"

原文地址:https://www.cnblogs.com/javasl/p/11155138.html