First, the use of ((expression))
root@root:~# a=3
root@root:~# ((b=a+3))
root@root:~# echo $b
6
The second use let
root@root:~# let "c=$a+4"
root@root:~# echo $c
7
Use expr expression, note the space and backtick
root@root:~# d=`expr 4 + 3`
root@root:~# echo $d
7
On $ [expression], which is bash the recommended standard treatment
root@root:~# a=4
root@root:~# b=$[$a-9]
root@root:~# echo $b
-5
In the for-loop style C, may be used as follows
root@root:~# for((a=1,b=10;a<=10;a++,b--))
do
((c=a*b))
echo "$c
"
done
10
18
24
28
30
30
28
24
18
10
Floating-point operations, with bc (bash calculator) operation
基本格式: variable=`echo "option;expression" | bc`
root@root:~# a=`echo "scale=4;3.25/3" | bc`
root@root:~# echo $a
1.0833
root@root:~# a=6.6
root@root:~# b=3.5
root@root:~# c=`echo "scale=5;$a/$b"|bc`
root@root:~# echo $c
1.88571
If a floating-point expression is more than one line, you can put multiple lines computing
基本格式:
variable=`bc << EOF
options
statements
expressions
EOF
`
演示:
root@root:~# a=1.2
root@root:~# b=2.4
root@root:~# c=3.5
root@root:~# d=2
root@root:~# e=`bc << EOF
scale=4
ab=($a+$b)
cd=($c-$d)
ab+cd
EOF
`
root@root:~# echo $e
5.1
Linux bash in operation
Guess you like
Origin www.cnblogs.com/xhai/p/11369936.html
Recommended
Ranking