var1=5
var2=3
var3=`expr $var1 + $var2`
echo $var3
输出结果 8
var1=5
var2=3
var3=$(($var1 + $var2))
echo $var3
输出结果 8
num1=100
num2=200
expr $num1 \| $num2
expr $num1 \& $num2
expr $num1 \< $num2
expr $num1 \> $num2
expr $num1 \<= $num2
expr $num1 \>= $num2
expr $num1 = $num2
expr $num1 != $num2
expr $num1 + $num2
expr $num1 - $num2
expr $num1 \* $num2
expr $num1 / $num2
expr $num1 % $num2
输出结果
100
100
1
0
1
0
0
1
300
-100
20000
0
100
稍微复杂点的运算
read -p "please input a number: " num
num1=0
for ((i=1;i<$num;i++))
do
if [ `expr $i % 2` -eq 1 ];then
echo $i
num1=`expr $num1 + $i`
fi
done
echo $num1
网友评论