Introduction
How many of you like mathematics?
Maybe maths isn’t your favourite subject, but you may have a knack for it due to its extensive usefulness.
When we were kids, say around 7 or 8 years old, we were taught the basic operations of maths. Then, we only knew addition and subtraction, and soon after learned multiplication and division.
A few years later, we learned boolean algebra with operators like AND, OR, and NOT.
So, different kinds of mathematics have different basic operators.
Similarly, in Linux, different shells have different operators.
The default shell in Linux is the Bourne Shell or Bash. In this article, we’ll learn about the basic operators in Linux in this default shell.

Basic Operators in Linux
Just as in mathematics, we have basic operators like addition, subtraction, multiplication, and division, there are certain basic operators in Linux too. Those basic operators are
(i) Arithmetic operators to perform normal mathematical operations
(ii) Relational operators to define the relationship between two operands
(iii) Logical/Boolean operators to perform logical operations
(iv) Bitwise operators to perform Bitwise operations on bit patterns
(v) File test operators to test the properties of a file
(vi) String operators to test the properties of a string

Let’s see what they are in detail in the subsequent sections.
Arithmetic operators
Arithmetic operators perform the basic arithmetical operations like addition, subtraction, etc. The arithmetic operators in Linux are described in the table below.
Operator |
Example |
Additions (+): Adds the operands |
Code:
Output: |
Subtraction (-): Subtracts the operands |
Code:
Output: |
Multiplication (*): Multiplies the operands |
Code:
Output: |
Division (/): Divides the operands |
Code:
Output: |
Modulus (%): Returns the remainder after division |
Code:
Output: |
Assignment (=): Assigns the value of the right-hand operand in the left-hand one |
Code:
Output: |
Increment (++): Increases the value of the operand by 1 |
Code:
Output: |
Decrement: Decreases the value of the operand by 1 |
Code:
Output: |
Relational Operators
Relational operators give the relation between two operands. In Linux, these operands may be a numeric value such as 17 or even a string that is numeric like “17”. The output given by this operator is either true (1) or false (0).
The relational operators in Linux are described below.
Operators |
Example |
Equality (==): Checks if two operands are equal |
Code:
Output: |
Not equality (!=): Checks if two operands are not equal |
Code:
Output: |
Less than (<): Checks if the first operand is less than the second |
Code:
Output: |
Less than or equal to (<=): Checks if the first operand is less than or equal to the second |
Code:
Output: |
Greater than (>): Checks if the first operand is greater than the second |
Code:
Output: |
Greater than or equal to (>=): Checks if the first operand is greater than or equal to the second |
Code:
Output: |
Logical or Boolean Operators
The logical or boolean operators in Linux are the same as the operators we know in Boolean algebra.
Still, let us see what they are.
Note: The operands for the Boolean operators must be either 0 or 1.
Operator |
Example |
AND (&&): Returns true (1)if both the operands are true (1) |
Code:
Output: |
OR (||): Returns true (1) if either of the operands is true (1) |
Code:
Output: |
NOT (!): Returns true if the operand is false and false if the operand is true |
Code:
Output: |
Bitwise Operators
As the name suggests, bitwise operators are used to operate on bit patterns. They are of six types, as shown below.
Operator |
Example |
Bitwise AND (&): Performs the standard AND operation on each bit |
Code:
Output:
Calculation: a = 00000100 b = 00000000 a&b = 00000000 = 0 |
Bitwise OR (|): |
Code:
Output:
Calculation: a = 00000100 b = 00000001 a | b = 00000101 = 5 |
Bitwise XOR (^): Performs the standard XOR operation on each bit |
Code:
Output:
Calculation: a = 00000100 b = 00000001 a ^ b = 00000101 = 5 |
Bitwise NOT (~): |
Code:
Output:
Calculation: a = 00001001 ~a = 11110110 = -10 |
Left shift (<<): |
Code:
Output:
Calculation: a = 00000001 a<<5 = 00010000 = 32 |
Right shift (>>): |
Code:
Output:
Calculation: a = 00010000 a&5 = 00000001 = 1 |
String Operators
String operators are operators used for files. They are used to check the different properties of strings.
Operator |
Example |
Equal (=): Checks if two strings are equal |
Code:
Output: |
Not equal (!=): Checks if two strings are unequal |
Code:
Output: |
-z string: Checks if the size of a string is zero |
Code:
Output: |
-n string: Checks if the size of a string is non-zero |
Code:
Output: |