There are some limitations to mathematical operations in the shell. It is mainly divided into the following types:
1. square brackets-------support only integer arithmetic
[[email protected] documents]$ var1=$[1+5]
[email protected] documents]$ echo $var 1
6
[Email protected] documents]$ var2=$[$var 1*2]
[Email protected] documents]$ echo $var 2
12
[Email protected] documents]$ var1=100
[Email protected] documents]$ var2=45
[[email protected] documents]$ var3=$[$var 1/$var 2]
[Email protected] documents]$ echo $var 3
2
2. Floating point Solution
Use the built-in bash calculator directly, called BC. It supports multiple types of calculations
Floating-point computing is designed to be a problem of precision, and you can set the value of the variable scale to fit the precision we want. For example, scale=4, meaning that 4-bit output is retained after the decimal point
BC can handle other data types, time reference.
Reference Documents :
Linux command line and Shell Scripting Encyclopedia (3rd edition) [Mei] Bloom (Richard Blum), Bresnahan (Christine Bresnahan), Jia, Wuhai
Shell Basics 03 Mathematical operations