Basic Shell operators and Shell Operators
Native bash does not support simple mathematical operations, but can be implemented using other commands, such as awk and expr, which are most commonly used. Expr is an expression computing tool that can be used to evaluate an expression. For example, the two numbers are added together (the quotation marks are used instead of single quotation marks '):
#! /Bin/bashval = 'expr 2 + 2' echo "the sum of the two values: $ val"
Run the script and the output result is as follows:
Sum of two numbers: 4
Note:
- There must be spaces between expressions and operators. For example, 2 + 2 is incorrect and must be written as 2 + 2.
- The complete expression must be included in ''. Note that this character is not a common single quotation mark.
The following table lists commonArithmetic OperatorsAssume that variable a is 10 and variable B is 20:
Note:
- A backslash (\) must be added to the front side of the multiplication sign (*) to realize multiplication.
- The conditional expression must be placed between square brackets with spaces.
Relational operatorsOnly numbers are supported. strings are not supported unless the value of a string is a number. The following table lists commonly used Relational operators. Assume that variable a is 10 and variable B is 20.
The following table lists commonBoolean operatorAssume that variable a is 10 and variable B is 20.
The following describesLogical operatorsAssume that variable a is 10 and variable B is 20.
The following table lists commonString OperatorsAssume that variable a is "abc" and variable B is "efg"
File test operatorUsed to detect various properties of Unix files