Linux中的数值运算

方法1:
  declare -i 变量=$变量1+$变量2
    a.变量和=之间不能有空格
    b.变量和+之间不能有空格
[root@localhost ~]# a=1
[root@localhost ~]# b=2
[root@localhost ~]# declare -i c=$a+$b
[root@localhost ~]# echo $c
3

方法2:
  变量=$(expr $变量 + $变量)
    a.=左右两边不能有空格
    b.+左右两边必须有空格
[root@localhost ~]# a=1
[root@localhost ~]# b=2
[root@localhost ~]# c=$(expr $a + $b)
[root@localhost ~]# echo $c
3

方法3:
  变量=$((运算式))
    a.=左右两边不能有空格
    b.运算式随便写,很自由
[root@localhost ~]# a=1
[root@localhost ~]# b=2
[root@localhost ~]# c=$(($a + $b))
[root@localhost ~]# echo $c
3

方法3:
  变量=$[运算式]
    a.=左右两边不能有空格
    b.运算式随便写,很自由
[root@localhost ~]# a=1
[root@localhost ~]# b=2
[root@localhost ~]# c=$[$a + $b + 1]
[root@localhost ~]# echo $c
4

原文地址:https://www.cnblogs.com/413xiaol/p/7158216.html