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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM