This article is reproduced in: http://blog.sina.com.cn/s/blog_674b5aae0100o2mn.html
BASH Shell has four arithmetic operations:
1: Use expr external program
Addition r = 'expr 4 + 5'
Echo $ R
Note!
4 + 5 there must be gaps between the three
'Expr 4 + 5'
Both the start and end of a statement have a ', and ~ Located in the same key
R = 'expr 4 * 5' # error
Multiplication r = 'expr 4 \ * 5'
2: use $ (())
R = $(4 + 5 ))
Echo $ R
3: use $ []
R = $[4 + 5]
Echo $ R
Multiplication
R = 'expr 4 \ * 5'
R = $(4*5 ))
R = $ [4*5]
Echo $ R
Division
R = 'expr 40/5'
R = $(40/5 ))
R = $ [40/5]
Echo $ R
Subtraction
R = 'expr 40-5'
R = $(40-5 ))
R = $ [40-5]
Echo $ R
Returns the remainder.
R = $ [100% 43]
Echo $ R
Power (such as power 3 of 2)
R = $(2 ** 3 ))
R = $[2 ** 3]
Echo $ R
Note: expr has no power.
4: Use the let Command
Addition:
N = 10
Let n = n + 1
Echo $ N # n = 11
Multiplication:
Let M = N * 10
Echo $ m
Division:
Let R = m/10
Echo $ R
Calculate the remainder:
Let R = m % 7
Echo $ R
Multiplication limit:
Let R = m ** 2
Echo $ R
Although bash shell has four arithmetic operations, not each of them is cross-platform. We recommend that you use expr.
In addition, we often haveAdd 1 Operation, The following four methods can be used:
M = $ [M + 1]
M = 'expr $ m + 1'
M = $ ($ m + 1 ))
Let M = m + 1