There are various operators supported by bash.
Arithmetic operators
Bash does not originally support simple arithmetic operations, but it uses external programs, either awk or expr.
Assume variable a holds 10 and variable b holds 20.
| Operator | Description | Example |
|---|---|---|
| + (Addition) | Adds values on either side of the operator | expr $a + $b will give 30 |
| - (Subtraction) | Subtracts right hand operand from left hand operand | expr $a - $b will give -10 |
| * (Multiplication) | Multiplies values on either side of the operator | expr $a \* $b will give 200 |
| / (Division) | Divides left hand operand by right hand operand | expr $b / $a will give 2 |
| % (Modulus) | Divides left hand operand by right hand operand and returns remainder | expr $b % $a will give 0 |
| = (Assignment) | Assigns right operand in left operand | a = $b would assign value of b into a |
| == (Equality) | Compares two numbers, if both are same then returns true. | [ b ] would return false. |
| != (Not Equality) | Compares two numbers, if both are different then returns true. | [ b ] would return true. |
Relational operators
Bash support the following relational operators that are specific to numeric values. Assume variable a holds 10 and variable b holds 20.
| Operator | Description | Example |
|---|---|---|
| -eq | Checks if the value of two operands are equal or not; if yes, then the condition becomes true. | [ b ] is not true. |
| -ne | Checks if the value of two operands are equal or not; if values are not equal, then the condition becomes true. | [ b ] is true. |
| -gt | Checks if the value of left operand is greater than the value of right operand; if yes, then the condition becomes true. | [ b ] is not true. |
| -lt | Checks if the value of left operand is less than the value of right operand; if yes, then the condition becomes true. | [ b ] is true. |
| -ge | Checks if the value of left operand is greater than or equal to the value of right operand; if yes, then the condition becomes true. | [ b ] is not true. |
| -le | Checks if the value of left operand is less than or equal to the value of right operand; if yes, then the condition becomes true. | [ b ] is true. |
Boolean operators
The following Boolean operators are supported by the Bourne Shell. Assume variable a holds 10 and variable b holds 20.
| Operator | Description | Example |
|---|---|---|
| ! | This is logical negation. This inverts a true condition into false and vice versa. | [ ! false ] is true. |
| -o | This is logical OR. If one of the operands is true, then the condition becomes true. | [ b -gt 100 ] is true. |
| -a | This is logical AND. If both the operands are true, then the condition becomes true otherwise false. | [ b -gt 100 ] is false. |
Operating_system Bash Bash_scripting Bash_programming Bash_operators