Bash shell的算术运算有四种方式:
1:使用 expr 外部程式

加法 r=`expr 4 + 5`
echo $r
注意! '4' '+' '5' 这三者之间要有空白
r=`expr 4 * 5` #错误
乘法 r=`expr 4 \*5`

2:使用 $(( ))
r=$(( 4 + 5 ))
echo $r

3:使用 $[ ]

r=$[ 4 + 5 ]
echo $r

乘法
r=`expr 4 \* 5`
r=$(( 4 * 5 ))
r=$[ 4 * 5 ]
echo $r

除法
r=`expr 40 / 5`
r=$(( 40 / 5 ))
r=$[ 40 / 5 ]
echo $r

减法
r=`expr 40 - 5`
r=$(( 40 - 5 ))
r=$[ 40 - 5 ]
echo $r

求余数
r=$[ 100 % 43 ]
echo $r

乘幂 (如 2 的 3次方)
r=$(( 2 ** 3 ))
r=$[ 2 ** 3 ]
echo $r
注:expr 沒有乘幂

4:使用let 命令
加法:
n=10
let n=n+1
echo $n #n=11

乘法:
let m=n*10
echo $m

除法:
let r=m/10
echo $r


求余数:
let r=m%7
echo $r


乘冪:
let r=m**2
echo $r

虽然Bash shell 有四种算术运算方法,但并不是每一种都是跨平台的,建议使用expr。
另外,我们在 script 中经常有加1操作,以下四法皆可:
m=$[ m + 1]
m=`expr $m + 1`
m=$(($m + 1))
let m=m+1

#!/bin/bash
#FileName:sh04.sh
#History:20-11-2013
#Function:It will show you tow numbers cross.
PATH=/bin/
export PATH
echo -e "\n You SHOULD input tow numbers ,I will cross them._"
read -p "First Number:_"firstnu
read -p "Second Number:_"secondnu
total=$(($firstnu * $secondnu))
total_1=$(($firstnu * $secondnu))
total_2=$(($firstnu / $secondnu))
total_3=$(($firstnu +  $secondnu))
total_4=$(($firstnu -  $secondnu))
echo -e "\n The score of $firstnu x $secondnu = $total_1\n"
echo -e "\n The score of $firstnu / $secondnu = $total_2\n"
echo -e "\n The score of $firstnu + $secondnu = $total_3\n"
echo -e "\n The score of $firstnu - $secondnu = $total_4\n"
测试:
$sh test.sh
-e
 You SHOULD input tow numbers ,I will cross them._
First Number:_6
Second Number:_4
-e
 The score of 6 x 4 = 24
-e
 The score of 6 / 4 = 1
-e
 The score of 6 + 4 = 10
-e
 The score of 6 - 4 = 2