the assignment and operation in the shell is the string processing by default, here are some special methods of mathematical operation in the shell, which can be seen later, 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
Output is the same, still tragic, hehe
2. Correct method
1) Use Let
Var=1
Let "Var+=1"
Echo $var
The output is 2, no tragedy this time.
Attention:
A) I tested let almost support all operators, see an article on the internet said "let does not support + + 、--and comma, (,)", but after I test the self-add, subtract, and the precedence of parentheses are well supported
b) Power operation should use "* *"
c) parameters are accessed directly in the expression without adding $
d) In general, the arithmetic expression can be without double quotes, but if the expression has a keyword in bash, you need to add
e) The Let expression can only be integer-arithmetic
2) use (())
Var=1
((var+=1))
Echo $var
Output is 2
Attention:
(()) is used in exactly the same way 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 accessing variables in $[], you need to add $ before
c) $[] supported operators are the same as let, but only integer operations are supported
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) expr also supports integer arithmetic only
5) use BC (floating point calculation is possible)
Var=1
Var= ' echo ' $var +1 "|BC"
Echo $var
Output is 2
Introduced:
BC is a simple calculator under Linux, support floating point calculation, enter BC at the command line into the calculator program, and we want to directly in the program floating point calculation, the use of a simple pipeline to solve the problem.
Attention:
1) After 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 and a programming language, and 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 calculate floating-point numbers.
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 put all the information can be collected to read, and finally summed up a set of their own things, and later encountered similar problems have looked, haha ~
Mathematical operations in shell scripts