shell中的数值计算:4种方式

$(($i+$j))
$[$i+$j]
let $i+$j 
expr $i + $j 	注意空格

案例1:

[root@docker01 practice_for]# sum=0
[root@docker01 practice_for]# echo $sum
0
[root@docker01 practice_for]# for i in {1..100..2};do sum=$(($sum+$i));done;echo $sum;
2500

案例2:

[root@docker01 practice_for]# sum=0
[root@docker01 practice_for]# for i in {1..100..2};do sum=$[$sum+$i];done;echo $sum;
2500

案例3:

[root@docker01 practice_for]# sum=0
[root@docker01 practice_for]# for i in {1..100..2};do let sum=$sum+$i;done;echo $sum;
2500

案例4(不推荐):

[root@docker01 practice_for]# sum=0
[root@docker01 practice_for]# for i in {1..100..2};do sum=$(expr $sum + $i);done;echo $sum;
2500

你可能感兴趣的:(Linux,linux)