方法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
