shell中的整数运算包括,加减乘除取余,+,-,*(*是元字符,表示任意一个字符,所以需要转义),/,%。
方法一:expr
[root@#test test]# expr 1 + 2
3
[root@#test test]# a=1
[root@#test test]# b=3
[root@#test test]# expr $a + $b
4
注意:运算符(+,-,*,/)前后一定要有空格!!!
方法二:$(())
[root@#test test]# num1=10
[root@#test test]# num2=20
[root@#test test]#
[root@#test test]# echo $(($num1+$num2))
30
[root@#test test]# echo $((num1+num2))
30
[root@#test test]# echo $((2*5-3))
7
[root@#test test]# result=$((2+3))
[root@#test test]# echo $result
5
方法三:$[]
同$(())类似
[root@#test test]# num1=10
[root@#test test]# num2=20
[root@#test test]# echo $[num1+num2]
30
[root@#test test]# echo $[2*5-9]
1
方法四:let
[root@#test test]# let c=1+2
[root@#test test]# echo $c
3
[root@#test test]#
[root@#test test]# a=1
[root@#test test]# b=3
[root@#test test]# let c=$a+$b
[root@#test test]# echo $c
4
方法五:bc命令支持小数运算
需要先安装:yum install bc
[root@#test test]# echo "23.89+45.9877"|bc
69.8777
[root@#test test]#
[root@#test test]# echo "1+2"|bc
3