Table of contents
1.
Introduction
2.
Basic Operators in Linux
2.1.
Arithmetic operators
2.2.
Relational Operators
2.3.
Logical or Boolean Operators
2.4.
Bitwise Operators
2.5.
String Operators
3.
Frequently Asked Questions
3.1.
What are the basic operators in Linux?
3.2.
What is the default shell in Linux?
3.3.
What operands do bitwise operators work with?
3.4.
What is the use of file test operators?
3.5.
What are string operators in Linux?
4.
Conclusion
Last Updated: Mar 27, 2024
Easy

Linux - Basic Operators

Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

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.

Mathematical operators in the background with "Linux - Basic Operators" written over them

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

"Basic operators in Linux" written in a circular shape and "Arithmetic Operators," "Bitwise Operators," "File Test Operators," "String Operators," "Logical Operators," and "Relational Operators" written in stars around it.

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:

a=1
b=7
echo "a + b = $((a + b))"

 

Output:

 

Subtraction (-): 

Subtracts the operands

Code:

a=1
b=7
echo "a - b = $((a - b))"

 

Output:

 

Multiplication (*): 

Multiplies the operands

Code:

a=1
b=7
echo "a * b = $((a * b))"

 

Output:

 

Division (/): 

Divides the operands

Code:

a=1
b=7
echo "a / b = $((a / b))"

 

Output:

 

Modulus (%): 

Returns the remainder after division

Code:

a=1
b=7
echo "a % b = $((a % b))"

 

Output:

 

Assignment (=): 

Assigns the value of the right-hand operand in the left-hand one

Code:

a=1
b=7
echo "b = $((b))"
b=$a
echo "b = $((b))"

 

Output:

 

Increment (++): 

Increases the value of the operand by 1

Code:

a=1
echo ”a = $((a))”
echo "++a = $((++a))"

 

Output:

 

Decrement: 

Decreases the value of the operand by 1

Code:

a=1
echo “a = $((a))”
echo "--a = $((--a))"

 

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:

a=1
b=7
#The output will be 0 which means false
echo "$((a==b))"

 

Output:

 

Not equality (!=): 

Checks if two operands are not equal

Code:

a=1
b=7
#The output will be 1 which means true
echo "$((a!=b))"

 

Output:

 

Less than (<): 

Checks if the first operand is less than the second

Code:

a=1
b=7
#The output will be 1 which means true
echo "$((a<b))"

 

Output:

 

Less than or equal to (<=): 

Checks if the first operand is less than or equal to the second

Code:

a=1
b=7
#The output will be 1 which means true
echo "$((a<=b))"

 

Output:

 

Greater than (>): 

Checks if the first operand is greater than the second

Code:

a=1
b=7
#The output will be 0 which means false
echo "$((a>b))"

 

Output:

 

Greater than or equal to (>=): 

Checks if the first operand is greater than or equal to the second

Code:

a=1
b=7
#The output will be 0 which means false
echo "$((a>=b))"

 

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:

a=1
b=1
#The output will be 1 which means true
echo "$((a&&b))"
a=0
b=1
#The output will be 0 which means false
echo "$((a&&b))"

 

Output:

 

OR (||):

Returns true (1) if either of the operands is true (1)

Code:

a=1
b=0
#The output will be 1 which means true
echo "$((a||b))"
a=0
b=0
#The output will be 0 which means false
echo "$((a||b))"

 

Output:

 

NOT (!): 

Returns true if the operand is false and false if the operand is true

Code:

a=1
b=0
#The output will be 1 which means true
echo "$((!b))"
#The output will be 0 which means false
echo "$((!a))"

 

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:

a=0x4
b=0x0
echo "a & b = $((a & b))"

 

Output:

 

Calculation: a = 00000100

b = 00000000

a&b = 00000000 = 0

 

Bitwise OR (|):
Performs the standard OR operation on each bit

Code:

a=0x4
b=0x1
echo "a | b = $((a | b))"

 

Output:

 

Calculation: a = 00000100

b = 00000001

a | b = 00000101 = 5

 

Bitwise XOR (^):

Performs the standard XOR operation on each bit

Code:

a=0x4
b=0x1
echo "a ^ b = $((a ^ b))"

 

Output:

 

Calculation: a = 00000100

b = 00000001

a ^ b = 00000101 = 5

 

Bitwise NOT (~):
Performs the standard NOT operation on each bit

Code:

a=0x9
echo "~a = $(( ~a ))"

 

Output:

 

Calculation: a = 00001001

~a = 11110110 = -10

 

Left shift (<<):
Shifts each bit to the left 

Code:

a=0x1
echo "a << 5 = $((a<<5))"

 

Output:

 

Calculation: a = 00000001

a<<5 = 00010000 = 32

 

Right shift (>>):
Shifts each bit to the right

Code:

a=0x32
echo "a >> 5 = $((a>>5))"

 

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:

a="Coding"
b="Ninjas"
#Output is 0 which means false
echo "$((a = b))"

 

Output:

 

Not equal (!=):

Checks if two strings are unequal

Code:

a="Coding"
b="Ninjas"
#Output is 1 which means true
echo "$((a != b))"

 

Output:

 

-z string:

Checks if the size of a string is zero

Code:

a="Coding"
if [ -z $a ]
then
    echo "True"
else
    echo "False"
fi

 

Output:

 

-n string:

Checks if the size of a string is non-zero

Code:

a="Coding"
if [ -n $a ]
then
    echo "True"
else
    echo "False"
fi

 

Output:

Frequently Asked Questions

What are the basic operators in Linux?

The basic operators in Linux are divided into six categories: arithmetic, relational, logical/Boolean, file test, and string operators. 

What is the default shell in Linux?

The default shell in Linux is the Bourne Shell or Bash.

What operands do bitwise operators work with?

Bitwise operators are used to operate on bit patterns.

What is the use of file test operators?

File test operators are used to check the different properties of files. 

What are string operators in Linux?

String operators are operators used for files. They are used to check the different properties of strings.

Conclusion

With this, we have finished learning about the basic operators in Linux. We learned about the six different types of operators and which operators fall under them. There we also some examples of each. Are we done yet, though?

Definitely not!

The basic operators in Linux are just a tiny part of it. Apart from it, there are lots more to learn about, but how will we do that?

Don’t worry, as Coding Ninjas has got you covered with the basics of Linux curated in one place, Coding Ninjas Studio

Apart from that, don’t forget to check out Data Structures and AlgorithmsCompetitive ProgrammingBasics of C++Data Structures and Algorithms in C++, and many more courses on Coding Ninjas.

Happy learning!

Live masterclass