1. Examples of error methods
a)
var=1+1
echo $var
The output result is 1+1, tragedy, haha
b)
var=1
var=$var+1
echo $var
The output result is 1+1, it is still tragic, haha
2. The correct way
1) Use let
var=1
let "var+=1"
echo $var
The output is 2, there is no tragedy this time
Notice:
a) After I tested let's support almost all operators. I saw an article online saying "let does not support ++, -- and commas, (,)", but after I tested the priority of self-addition, self-deduction, and brackets are all well supported.
b) The square exponent operation should use "**"
c) Parameters are accessed directly in expressions without adding $
d) Generally, arithmetic expressions can be without double quotes, but if there are keywords in bash in the expression, you need to add them
The expression after e)let can only perform integer operation
2) Use (())
var=1
((var+=1))
echo $var
The output result is 2
Notice:
(()) is used exactly the same as let
3) Use $[]
var=1
var=$[$var+1]
echo $var
Output result bit 2
Notice:
a)$[] Take the expression in brackets as a mathematical operation to calculate the result first and then output it
b) When accessing variables in $[], you need to add $ in advance
c) The operator supported by $[] is the same as let, but it only supports integer operation.
4) Use expr
var=1
var=`expr $var + 1`
echo $var
The output result is 2
Notice:
a) The expression symbols after expr must be separated by spaces.
b) The operators supported by expr are: |, &, <, <=, =, !=, >=, >, +, -, *, /, %
c) The operators supported by expr need to be escaped with \ when used: |, &, <, <=, >=, >, *
e)expr also only supports integer operation
5) Use bc (can perform floating point calculation)
var=1
var=`echo "$var+1"|bc`
echo $var
The output result is 2
introduce:
bc is a simple calculator under Linux, which supports floating point calculation. Enter bc on the command line and enter the calculator program. When we want to directly calculate floating point numbers in the program, we can solve the problem by using a simple pipeline.
Notice:
1) After I tested that bc supports all operators except bit operation operators.
2) Use scale to set the accuracy in bc
3) Floating point number calculation example
var=3.14
var=`echo "scale=2;$var*3"|bc`
echo $var
The output is 9.42
6) Use awk (floating point calculation can be performed)
var=1
var=`echo "$var 1"|awk '{printf("%g",$1*$2)}'`
echo $var
The output result is 2
introduce:
awk is a text processing tool and also a programming language. As a programming language, awk supports multiple operations, and we can use awk to perform floating-point number calculations. Like bc above, through a simple pipeline, we can directly call awk in the program for floating-point number calculations.
Notice:
1) awk supports all operators except micro-operation operators
2) Awk has built-in functions such as log, sqr, cos, sin, etc.
3) Floating point number calculation example
var=3.14
var=`echo "$var 2"|awk '{printf("%g",sin($1/$2))}'`
echo $var
The output result is 1
3. Summary
Finally, I finished reading all the collected information and finally summarized a set of my own things. In the future, I would read some of them when I encountered similar problems. Haha~
Yorking Alan
a)
var=1+1
echo $var
The output result is 1+1, tragedy, haha
b)
var=1
var=$var+1
echo $var
The output result is 1+1, it is still tragic, haha
2. The correct way
1) Use let
var=1
let "var+=1"
echo $var
The output is 2, there is no tragedy this time
Notice:
a) After I tested let's support almost all operators. I saw an article online saying "let does not support ++, -- and commas, (,)", but after I tested the priority of self-addition, self-deduction, and brackets are all well supported.
b) The square exponent operation should use "**"
c) Parameters are accessed directly in expressions without adding $
d) Generally, arithmetic expressions can be without double quotes, but if there are keywords in bash in the expression, you need to add them
The expression after e)let can only perform integer operation
2) Use (())
var=1
((var+=1))
echo $var
The output result is 2
Notice:
(()) is used exactly the same as let
3) Use $[]
var=1
var=$[$var+1]
echo $var
Output result bit 2
Notice:
a)$[] Take the expression in brackets as a mathematical operation to calculate the result first and then output it
b) When accessing variables in $[], you need to add $ in advance
c) The operator supported by $[] is the same as let, but it only supports integer operation.
4) Use expr
var=1
var=`expr $var + 1`
echo $var
The output result is 2
Notice:
a) The expression symbols after expr must be separated by spaces.
b) The operators supported by expr are: |, &, <, <=, =, !=, >=, >, +, -, *, /, %
c) The operators supported by expr need to be escaped with \ when used: |, &, <, <=, >=, >, *
e)expr also only supports integer operation
5) Use bc (can perform floating point calculation)
var=1
var=`echo "$var+1"|bc`
echo $var
The output result is 2
introduce:
bc is a simple calculator under Linux, which supports floating point calculation. Enter bc on the command line and enter the calculator program. When we want to directly calculate floating point numbers in the program, we can solve the problem by using a simple pipeline.
Notice:
1) After I tested that bc supports all operators except bit operation operators.
2) Use scale to set the accuracy in bc
3) Floating point number calculation example
var=3.14
var=`echo "scale=2;$var*3"|bc`
echo $var
The output is 9.42
6) Use awk (floating point calculation can be performed)
var=1
var=`echo "$var 1"|awk '{printf("%g",$1*$2)}'`
echo $var
The output result is 2
introduce:
awk is a text processing tool and also a programming language. As a programming language, awk supports multiple operations, and we can use awk to perform floating-point number calculations. Like bc above, through a simple pipeline, we can directly call awk in the program for floating-point number calculations.
Notice:
1) awk supports all operators except micro-operation operators
2) Awk has built-in functions such as log, sqr, cos, sin, etc.
3) Floating point number calculation example
var=3.14
var=`echo "$var 2"|awk '{printf("%g",sin($1/$2))}'`
echo $var
The output result is 1
3. Summary
Finally, I finished reading all the collected information and finally summarized a set of my own things. In the future, I would read some of them when I encountered similar problems. Haha~
Yorking Alan