Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Last Updated: Mar 27, 2024
Difficulty: Easy

Shell Scripting Arithmetic Operations

Leveraging ChatGPT - GenAI as a Microsoft Data Expert
Speaker
Prerita Agarwal
Data Specialist @
23 Jul, 2024 @ 01:30 PM

Introduction

Shell provides an interface to use the OS to perform control other programs. It is a command interpreter and a programming language. It is used to communicate with the Linux system by  commands interpreter through the Linux/Unix shell. 

In this blog, we will discuss Arithmetic Operations in Shell Scripting. Let's start going!

Shell Scripting - Arithmetic Operations

Linux expr command 

The expr command works only with integers as well as a string. Even if they are numbers, all variables in shell scripts have a string value. So, we use the expr command to perform mathematical operations.

Syntax of expr command:-

$expr expression
Get the tech career you deserve, faster!
Connect with our expert counsellors to understand how to hack your way to success
User rating 4.7/5
1:1 doubt support
95% placement record
Akash Pal
Senior Software Engineer
326% Hike After Job Bootcamp
Himanshu Gusain
Programmer Analyst
32 LPA After Job Bootcamp
After Job
Bootcamp

Arithmetic Operators

Arithmetic Operator

An arithmetic Operator is a mathematical function that performs a calculation on two operands. They are used in everyday math, and most programming languages have a set that can be used inside equations to carry out various sequential calculations.

There are seven types of arithmetic operators defined in shell scripts. We will discuss each in brief.

Addition Operations

It is a binary operation used to add two operands. The (+) symbol represents it.

The given below example helps in understanding the addition of two numbers inputted by the user:-

#!/bin/sh

echo "Enter two numbers: "  #Taking input from the user
read x y

# perform addition
add=`expr $x + $y`

# Addition of two numbers
echo " The sum of the two numbers is: $add"

 

Output

$ sh add.sh
Enter two numbers: 
5 8
The sum of the two numbers is: 13

Subtraction  Operations

It is a binary operation used to subtract two operands. The (-) symbol represents it.

The given below example helps in understanding the subtraction of two numbers inputted by the user:-

#!/bin/sh

echo "Enter two numbers: " # Taking input from the user
read x y

# perform subtraction
sub =`expr $x - $y`

# Subtraction of two numbers
echo " The subtraction of the two numbers is: $sub"

 

Output

$ sh sub.sh
Enter two numbers: 
13 2
The subtraction of the two numbers is: 11

Multiplication Operator

It is a binary operation used to multiply two operands. It is represented by the (*) symbol.

The symbol * in the shell stands for every file in the current directory. To use * as a multiplication operator, it must be used as ‘\*'.

The given below example helps in understanding the multiplication of two numbers inputted by the user:-

#!/bin/sh

echo "Enter two numbers: "  #Taking input from the user
read x y

# perform multiplication
mul=`expr $x \* $y`

# Multiplication of two numbers
echo " The multiplication of the two numbers is: $mul"

 

Output

$ sh mul.sh
Enter two numbers: 
11 3
The multiplication of the two numbers is: 33

Division Operator

It is a binary operation used to divide two operands. The (/) symbol represents it.

The given below example helps in understanding the division of two numbers inputted by the user:-

#!/bin/sh

echo "Enter two numbers: " #Taking input from the user
read x y

ans=`expr $x / $y `

# Division of two numbers
echo " The division of the two numbers is: $ans"

 

Output

$ sh div.sh
Enter two numbers: 
10 2
The division of the two numbers is: 5

Modulus Operator

It is a binary operation used to find the remainder of two operands. It is represented by the (%) symbol.

The given below example helps in understanding the modulus of two numbers inputted by the user:-

#!/bin/sh

echo "Enter two numbers: " #Taking input from the user
read x y

ans=`expr $x % $y `

# Modulus of two numbers
echo " The modulus of the two numbers is: $ans"


Output

$ sh mod.sh
Enter two numbers: 
10 2
The modulus of the two numbers is: 0

Increment Operator

A unary operator is used to increase the operand's value by one. It is of two types:-

Post-Increment arithmetic operator: It increases the variable's value after solving the expression. As a result, the value is first used in the expression before it is increased.

⭐ Pre-Increment arithmetic operator: It increases the variable's value before solving the expression. As a result, the value is used in the expression after it is increased.

Let’s understand with an example:

Example 1:

x=8
y=$((x++))  #post-increment 
echo $y
echo $x

 

Output

8
9

 

Example 2:

x=8
y=$((++x))  #pre-increment 
echo $y
echo $x

 

Output

9
9

Decrement Operator

A unary operator is used to decrease the operand's value by one. It is of two types:-

Post-decrement arithmetic operator: After solving the expression, it decreases the variable's value. As a result, the value is first used in the expression before it is decreased.

⭐ Pre-decrement arithmetic operator: It decreases the variable's value before solving the expression. As a result, the value is used in the expression after it is decreased.

Let’s understand with an example:

Example 1:

x=6
y=$((x–))  #post-decrement 
echo $y
echo $x

 

Output

6
5

 

Example 2:

x=6
y=$((--x))  #pre-decrement 
echo $y
echo $x

 

Output

5
5

Frequently Asked Questions

What is Shebang in a shell script?

Shebang combines the number sign (#) and "! ". This typically appears at the script or program's start or the very top. Developers typically use this to avoid tedious work. Shebang is primarily responsible for choosing the location of the engine that will be used to run the script.

Here, "#" is referred to as a hash, and "!" is referred to as a bang.

What is control instruction in the shell?

The ones that allow us to specify the order in which the various instructions in a program or script are to be executed by the computer are known as control instructions. They establish a program's control flow.

What does the statement Continue mean?

Continue is a keyword used to pass statements inside of the loop that hasn't yet been executed to return control to the beginning of the loop.

Conclusion

Congratulations on finishing the blog! We have discussed arithmetic operations in shell scripts. We further discussed the expr command and each arithmetic operation with examples in brief.

We hope this blog has helped you enhance your knowledge of Arithmetic Operations in Shell Scripting. Do not stop learning! We recommend you read some of our shell scripts articles: -

✴️ Introduction to Linux Shell & Scripting Shell

✴️ Shell Scripting Interview Questions

✴️ Crontab in Linux
 

Refer to our Guided Path to upskill yourself in DSACompetitive ProgrammingJavaScriptSystem Design, and many more! If you want to test your competency in coding, you may check out the mock test series and participate in the contests hosted on Coding Ninjas Studio!

To learn more about Micro Operations, refer to Arithmetic Micro Operations.

But suppose you have just started your learning process and are looking for questions from tech giants like Amazon, Microsoft, Uber, etc. In that case, you must look at the problemsinterview experiences, and interview bundles for placement preparations.

We wish you Good Luck! Please upvote our blog and help other ninjas grow.

Happy Learning!

Topics covered
1.
Introduction
2.
Linux expr command 
3.
Arithmetic Operators
3.1.
Addition Operations
3.2.
Subtraction  Operations
3.3.
Multiplication Operator
3.4.
Division Operator
3.5.
Modulus Operator
3.6.
Increment Operator
3.7.
Decrement Operator
4.
Frequently Asked Questions
4.1.
What is Shebang in a shell script?
4.2.
What is control instruction in the shell?
4.3.
What does the statement Continue mean?
5.
Conclusion