You can use, let
(())
[]
perform basic arithmetic operations, and use advanced operations bc
more efficiently.
Look directly at the code and the output is clear.
Let
[Zhengweiwu A: -~]$ no1=4[Zhengweiwu A: -~]$ no2=5[Zhengweiwu A: -~]$ Letresult=no1+no2[zhengweiwu A: -~]$ echo $result9[Zhengweiwu A: -~]$ Letresult=result+0.9 # Operators cannot have spaces on either side-bash:let: +: SyntaxError: Operand expected (ErrorToken is "+") [Zhengweiwu A: -~]$ Letresult=result+0.9 # Only integer operations can be performed-bash:let:result=result+0.9: syntaxError: Invalid arithmetic operator (ErrorToken is ". 9") [Zhengweiwu A: -~]$ echo $result9
[]
[zhengweiwu 22 : 26 ~]$ Result=$[ no1+no2][zhengweiwu 22 : 27 ~]$ echo $ Result 9 [Zhengweiwu 22 : 27 ~]$ result=$[ no1+no2+1 ][zhengweiwu 22 : 27 ~]$ echo $result 10
(())
[Zhengweiwu A: -~]$ result=$((1+1)) [Zhengweiwu A: -~]$ Echo$result2[Zhengweiwu A: -~]$ result=$((Ten+1)) [Zhengweiwu A: in~]$ Echo$result One
Bc
[zhengweiwu 22 : 29 ~]$ echo " 4*3.5 " | Bc14.0 [Zhengweiwu 22 : 29 ~]$ echo "4*3.54 " | Bc14.16 [Zhengweiwu 22 : 30 ~]$ result=$ (echo " $no 1 *0.44" | BC) # as a child shell calculation result [Zhengweiwu 22 : 30 ~]$ echo $result Span class= "Hljs-number" >1.76
[zhengweiwu 22 : 30 ~]$ echo " SCALE=2;3/8 " | BC # set decimal precision . 37 [Zhengweiwu 22 : 31 ~]$ echo "SCALE=4;3/8" | Bc. 3750 [Zhengweiwu 22 : 31 ~]$ echo "SCALE=6;3/8" | Bc. 375000
[zhengweiwu 22 : 31 ~]$ no=100 [Zhengweiwu 22 : 33 ~]$ echo "Obase = 2; $no " | BC # output is 2 binary, the default input is 10 binary 1100100 [Zhengweiwu Span class= "Hljs-number" >22 : 33 ~]$ no=1100100 [Zhengweiwu 22 : 33 ~]$ echo $no | BC # indicates output and input 100
22:34echo"sqrt(100)" | bc1022:35echo"10^10" | bc10000000000
Using the shell for mathematical operations