assignments and operations in the shell are string-handling by default, and here are a few special ways to do mathematical operations in the shell. In the future when you can see, hehe1. Examples of error methods
A
Var=1+1
Echo $var
The result of the output is the same, tragedy, hehe
b
Var=1
var= $var +1
Echo $var
The output is the same as the result. Still tragic, hehe
2. Correct method
1) Use Let
Var=1
Let "Var+=1"
Echo $var
The output result is 2. No tragedy this time.
Attention:
A) after I tested let almost support all operators, read an article on the internet that said "let does not support + + 、--and commas, (,)", but after my test self-add, subtract, and the priority of parentheses are very well supported
b) Power operation should use "* *"
c) The parameters are directly interviewed in the expression, do not need to add $
d) In general, the arithmetic expression can be no double-cited, but if the expression has keyword in bash, you need to add
e) The Let expression can only perform integer arithmetic
2) use (())
Var=1
((var+=1))
Echo $var
Output is 2
Attention:
The use of (()) is exactly the same as let
3) Use $[]
Var=1
var=$[$var +1]
Echo $var
Output result bit 2
Attention:
A) $[] The expression in parentheses is evaluated as a mathematical operation before the output
b) When visiting the variables in $[], you need to add $
c) $[] supports the same operators as let, but only supports integer operations
4) Using expr
Var=1
Var= ' expr $var + 1 '
Echo $var
Output is 2
Attention:
A) The expression after expr is separated by a space between the symbols
b) The operands supported by expr are: |, &, <, <=, =,! =, >=, >, + 、-、 *,/,%
c) The operands supported by expr need to be escaped when used: |, &, <, <=, >=, >, *
e) The same as expr only supports integer arithmetic
5) using BC (can be used for floating point calculation)
Var=1
Var= ' echo ' $var +1 "|BC"
Echo $var
Output is 2
Introduced:
BC is a simple calculator under Linux that supports floating-point calculation. Enter BC at the command line to enter the calculator program. When we want to calculate the floating-point numbers directly in the program, we can solve this problem by using a simple pipeline.
Attention:
1) I tested BC supports all operators except the bitwise operator.
2) Use scale for precision setting in BC
3) Floating-point Calculation example
var=3.14
Var= ' echo ' scale=2; $var "|BC"
Echo $var
Output is 9.42
6) using awk (floating point calculation is possible)
Var=1
Var= ' echo ' $var 1 "|awk ' {printf ("%g ", $1*$2)} '
Echo $var
Output is 2
Introduced:
Awk is a text processing tool. At the same time is also a programming language, as a programming language. AWK supports a number of operations, and we can use awk to calculate floating-point numbers, and, like the BC above, we can call awk directly in the program to do floating-point calculation in a simple pipeline.
Attention:
1) awk supports all operators except the micro operator
2) awk has built-in log, sqr, cos, sin, and other functions
3) Floating-point Calculation example
var=3.14
Var= ' echo ' $var 2 "|awk ' {printf ("%g ", Sin ($1/$2)}"
Echo $var
Output is 1
3. Summary
Finally, all the information can be collected to read, and finally summed up a set of their own things, and later encountered similar problems have seen, haha ~
Mathematical operations in shell scripts