Use shell for mathematical operations and shell mathematical operations
Availablelet
,(())
,[]
Basic arithmetic operations, advanced operationsbc
More efficient.
Let's look at the code and make the output clear.
Let
[Zhengweiwu ~] $ No1 = 4 [zhengweiwu ~] $ No2 = 5 [zhengweiwu ~] $ Let result = no1 + no2 [zhengweiwu ~] $ Echo $ result9 [zhengweiwu ~] $ Let result = result + 0.9 # There cannot be spaces on both sides of the operator-bash: let: +: syntax error: operand expected (error token is "+") [zhengweiwu ~] $ Let result = result + 0.9 # only integer operations can be performed-bash: let: result = result + 0.9: syntax error: invalid arithmetic operator (error token is ". 9 ") [zhengweiwu ~] $ Echo $ result9
[]
[zhengweiwu 22:26 ~]$ result=$[no1+no2][zhengweiwu 22:27 ~]$ echo $result9[zhengweiwu 22:27 ~]$ result=$[no1+no2+1][zhengweiwu 22:27 ~]$ echo $result10
(())
[zhengweiwu 22:28 ~]$ result=$((1+1))[zhengweiwu 22:28 ~]$ echo $result2[zhengweiwu 22:28 ~]$ result=$(( 10 +1))[zhengweiwu 22:29 ~]$ echo $result11
Bc
[Zhengweiwu ~] $ Echo "4*3.5" | bc14.0 [zhengweiwu ~] $ Echo "4*3.54" | bc14.16 [zhengweiwu ~] $ Result = $ (echo "$ no1 * 0.44" | bc) # used as the subshell computing result [zhengweiwu ~] $ Echo $ result1.76
[Zhengweiwu ~] $ Echo "scale = 2; 3/8" | bc # sets the decimal precision. 37 [zhengweiwu ~] $ Echo "scale = 4; 3/8" | bc.3750 [zhengweiwu ~] $ Echo "scale = 6; 3/8" | bc.375000
[Zhengweiwu ~] $ No = 100 [zhengweiwu ~] $ Echo "obase = 2; $ no" | bc # The output is in hexadecimal notation. The default input hexadecimal notation is in hexadecimal notation 1100100. [zhengweiwu ~] $ No = 1100100 [zhengweiwu ~] $ Echo "obase = 10; ibase = 2; $ no" | bc # specify the output hexadecimal and input hexadecimal 100
[zhengweiwu 22:34 ~]$ echo "sqrt(100)" | bc10[zhengweiwu 22:35 ~]$ echo "10^10" | bc10000000000