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.

OperatorDescriptionExample
+ (Addition)Adds values on either side of the operatorexpr $a + $b will give 30
- (Subtraction)Subtracts right hand operand from left hand operandexpr $a - $b will give -10
* (Multiplication)Multiplies values on either side of the operatorexpr $a \* $b will give 200
/ (Division)Divides left hand operand by right hand operandexpr $b / $a will give 2
% (Modulus)Divides left hand operand by right hand operand and returns remainderexpr $b % $a will give 0
= (Assignment)Assigns right operand in left operanda = $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.

OperatorDescriptionExample
-eqChecks if the value of two operands are equal or not; if yes, then the condition becomes true.[ b ] is not true.
-neChecks if the value of two operands are equal or not; if values are not equal, then the condition becomes true.[ b ] is true.
-gtChecks 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.
-ltChecks if the value of left operand is less than the value of right operand; if yes, then the condition becomes true.[ b ] is true.
-geChecks 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.
-leChecks 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.

OperatorDescriptionExample
!This is logical negation. This inverts a true condition into false and vice versa.[ ! false ] is true.
-oThis is logical OR. If one of the operands is true, then the condition becomes true.[ b -gt 100 ] is true.
-aThis is logical AND. If both the operands are true, then the condition becomes true otherwise false.[ b -gt 100 ] is false.

Operating_systemBashBash_scriptingBash_programmingBash_operators