The expr command takes an arithmetic expression as a parameter, usually in the following form:
expr [number] [operator] [number]
Because the shell stores variables as characters, the user must ensure that the operand that participates in the arithmetic operation must be numeric. The following are valid arithmetic operators:
+ two integers added
-first number minus second number
* Multiply two integers
/First integer divided by second integer
% two integers divide, take remainder
For example:
[Root@mail root]# expr 2 + 1
Results showed: 3
[Root@mail root]# expr 5-3
Results showed: 2
If one of the arguments to expr is a variable, replace the variable name with the variable value before the expression evaluates.
[Root@mail root]# int=3
[Root@mail root]# expr $int + 4
Results showed: 7
Users can not simply use "*" to do multiplication, if input:
[Root@mail root]# expr 4*5
The system will get an error, because the shell will see "*" to replace the file name first. The correct form is:
[[email protected] root]# Expr 4 \* 5
Results showed: 20
Multiple arithmetic expressions can be grouped together, for example:
[[email protected] root]# Expr 5 + 7/3
Results showed: 7
The order of operations is preceded by multiplication, and to change the order of operations, the "'" number must be used, such as:
[[email protected] root]# int= ' expr 5 + 7 '
[[email protected] root]# expr $int/3
Results showed: 4
Or:
[[email protected] root]# expr ' expr 5+7 '/3
Results showed: 4
Use of expr in the shell