I. Arithmetic Operators
**: Calculate the power of two variables.
Others: +-*/% + =-= * =/= % =
Ii. Operation Sequence
If the operation order is not changed without expr or $ [], the operation is assigned a value in the form of a string.
EHCO 1 + 2; the result will be 1 + 2 !!
Method 1: Use expr to represent the following expression as a mathematical operation
Note + there must be spaces before and after !! Otherwise, no arithmetic operation is performed.
Echo 'expr1+2`
Method 2: use $ [] to represent mathematical operations. Write a mathematical operation to the brackets of the $ [] symbol.
A = $ [$ A + 1]
3. A simple instance
Mathematical function S = 3 (x ^ y) + 4x ^ 2 + 5y + 6
Note: $1 and $2 are the values of the first and second variables respectively.
./Test. Sh 2 4
# ! /Bin/bash
# Test. Sh
S = 0 # Defines a sum variable. The initial value is 0.
T = 'expr $ 1 ** $ 2 ' # Use expr to change the operation order and obtain the power y of X.
T = $ [T * 3 ] # T multiplied by 3.
S = $ [S + T] # Result addition.
T = $ [$ 1 ** 2 ] # Returns the square of X.
T = $ [T * 4 ] # The result is multiplied by 4.
S = $ [S + T] # Result addition.
T = 'expr $ 2 * 5 ' # Evaluate the value of 5y.
S = $ [S + T] # Result addition.
S = $ [S + 6 ] # 6 is added to the result.
Echo $ S # Output result.