Do you think IIT Guwahati certified course can help you in your career?
No
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!
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
Arithmetic Operators
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: -
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 problems, interview experiences, and interview bundles for placement preparations.
We wish you Good Luck! Please upvote our blog and help other ninjas grow.