Learn Basic Mathematical Operations in Bash Scripting – Part IV

In this post, I will be discussing the scripts from a mathematical and numerical point of view. Although I posted a more complex script (Simple Calculator) in the previous post, it was difficult for users to understand. Hence, I thought I would teach you another useful aspect of learning in smaller parts.

Before this article, three articles in the Shell Scripting Series have been published and they are:

Let’s continue the learning process with exciting new shell scripts, starting with mathematical ones.

TecMint Weekly Newsletter
Get the Learn Linux 7 Days Crash Course free when you join 34,000+ Linux professionals reading every Thursday.
Check your email for a magic link to get started.
Something went wrong. Please try again.

1. Basic Addition Shell Script

In this script ‘addition.sh‘, you will create a shell script to perform basic addition operations.

vi addition.sh

Add the following code to addition.sh file.

#!/bin/bash
# This script adds two numbers provided by the user

echo "Enter the first number: "
read a
echo "Enter the second number: "
read b

sum=$((a + b))

echo "The sum of $a and $b is: $sum"

Next, make the script executable and run it.

chmod 755 addition.sh
./addition.sh
Basic Addition Shell Script
Basic Addition Shell Script

2. Basic Subtraction Shell Script

In this script ‘subtraction.sh‘, you will create a shell script to perform a basic subtraction operation.

vi subtraction.sh

Add the following code to subtraction.sh file:

#!/bin/bash
# This script subtracts two numbers provided by the user

echo "Enter the First Number: "
read a
echo "Enter the Second Number: "
read b

x=$(($a - $b))

echo "$a - $b = $x"

Next, make the script executable and run it.

chmod 755 subtraction.sh
./subtraction.sh
Basic Subtraction Shell Script
Basic Subtraction Shell Script

3. Basic Multiplication Shell Script

So far you would be enjoying a lot, learning scripts in such an easy way, so the next in chronological order is Multiplication.

vi multiplication.sh

Add the following code to multiplication.sh file.

#!/bin/bash
# This script multiplies two numbers provided by the user

echo "Enter the first number: "
read a
echo "Enter the second number: "
read b

product=$((a * b))

echo "The product of $a and $b is: $product"

Next, make the script executable and run it.

chmod 755 multiplication.sh
./multiplication.sh
Basic Multiplication Shell Script
Basic Multiplication Shell Script

4. Basic Division Shell Script

In this division script, it prompts the user to enter two numbers, then divides the first number by the second and displays the result.

vi division.sh

Add the following code to the division.sh file.

#!/bin/bash
echo "Enter the First Number: "
read a
echo "Enter the Second Number: "
read b
echo "$a / $b = $(expr $a / $b)"

Next, make the script executable and run it.

chmod 755 division.sh
./division.sh
Basic Division Shell Script
Basic Division Shell Script

5. Print Multiplication Table in Bash

In this script, we will print the multiplication table of a user-specified number. It prompts the user to enter a number and then displays the multiplication results from 1 to 10 for that number.

vi table.sh

Add the following code to the table.sh file.

#!/bin/bash
echo "Enter the number for which you want to print the table: "
read n
i=1
while [ $i -le 10 ]
do
  table=$(expr $i \* $n)
  echo "$i x $n = $table"
  i=$(expr $i + 1)
done

Next, make the script executable and run it.

chmod 755 table.sh
./table.sh
Print Multiplication Table in Bash
Print Multiplication Table in Bash

6. Check If a Number is Even or Odd in Bash

In this Bash script, we will determine whether a user-provided number is even or odd. It prompts the user to enter a number, checks its divisibility by 2 using the modulo operator, and then prints whether the number is even or odd based on the result.

vi evenodd.sh

Add the following code to evenodd.sh file.

#!/bin/bash
echo "Enter the number:"
read n
num=$(expr $n % 2)
if [ $num -eq 0 ]
then
  echo "$n is an Even Number"
else
  echo "$n is an Odd Number"
fi

Next, make the script executable and run it.

chmod 755 evenodd.sh
./evenodd.sh
Check If a Number is Even or Odd in Bash
Check If a Number is Even or Odd in Bash

7. Calculate Factorial Using Bash Script

This script calculates the factorial of a user-input number using a `while` loop, and then prints the result.

vi factorial.sh

Add the following code to the factorial.sh file.

#!/bin/bash
echo "Enter the number:"
read a
fact=1
while [ $a -gt 0 ]
do
  fact=$(expr $fact \* $a)
  a=$(expr $a - 1)
done
echo "The factorial is: $fact"

Next, make the script executable and run it.

chmod 755 factorial.sh
./factorial.sh
Calculate Factorial Using Bash Script
Calculate Factorial Using Bash Script

You can now relax knowing that calculating `12*11*10*9*8*7*6*5*4*3*2*1` is much simpler with the script above than doing it manually. Imagine needing to find `99!` – this script will be incredibly useful in such situations.

8. Check if a Number is an Armstrong Number in Bash

This Bash script determines whether a given three-digit number is an Armstrong number. An Armstrong number (or Narcissistic number) is a number where the sum of the cubes of its digits equals the number itself.

For example, 371 is an Armstrong number because 3×3×3+7×7×7+1×1×1 =371.

vi armstrong.sh

Add the following code to armstrong.sh file.

#!/bin/bash
echo "Enter a number:"
read n
arm=0
temp=$n
while [ $n -ne 0 ]
do
  r=$(expr $n % 10)
  arm=$(expr $arm + $r \* $r \* $r)
  n=$(expr $n / 10)
done
echo "Calculated sum of cubes: $arm"
if [ $arm -eq $temp ]
then
  echo "Armstrong"
else
  echo "Not Armstrong"
fi

Next, make the script executable and run it.

chmod 755 armstrong.sh
./armstrong.sh
Check if a Number is an Armstrong Number
Check if a Number is an Armstrong Number

9. Check if a Number is Prime in Bash

This Bash script checks if a number is prime by counting its divisors. It prints “Prime” if the number has exactly two divisors, and “Not Prime” otherwise.

vi prime.sh

Add the following code to prime.sh file.

#!/bin/bash
echo "Enter any number:"
read n

# Initialize variables
i=1
c=0

# Check for divisors
while [ $i -le $n ]
do
  if [ $(expr $n % $i) -eq 0 ]
  then
    c=$(expr $c + 1)
  fi
  i=$(expr $i + 1)
done

# Determine if the number is prime
if [ $c -eq 2 ]
then
  echo "Prime"
else
  echo "Not Prime"
fi

Next, make the script executable and run it.

chmod 755 prime.sh
./prime.sh
Check if a Number is Prime in Bash
Check if a Number is Prime in Bash

That’s all for now. In our next article, we will cover other mathematical programs in shell scripting. Don’t forget to share your thoughts in the comments section.

If this article helped, with someone on your team.

TecMint Weekly Newsletter
Get the Learn Linux 7 Days Crash Course free when you join 34,000+ Linux professionals reading every Thursday.
Check your email for a magic link to get started.
Something went wrong. Please try again.
TecMint has been free for 14 years. Help keep it that way.
Google AI Overviews and tools like ChatGPT have cut into search traffic for independent tech sites like TecMint. Running this site costs over $2,000 every month for hosting, infrastructure, and paying authors to keep the content accurate and tested.

If this article helped you solve a problem, consider buying a coffee. It helps keep TecMint free, supports the authors, and keeps the project going.
☕ Buy Me a Coffee
Ravi Saive
I'm Ravi Saive, an award-winning entrepreneur and founder of several successful 5-figure online businesses, including TecMint.com, GeeksMint.com, UbuntuMint.com, and the premium learning hub Pro.Tecmint.com.

Each tutorial at TecMint is created by a team of experienced Linux system administrators so that it meets our high-quality standards.

1 Comment

Leave a Reply

Got Something to Say? Join the Discussion...

Thank you for taking the time to share your thoughts with us. We appreciate your decision to leave a comment and value your contribution to the discussion. It's important to note that we moderate all comments in accordance with our comment policy to ensure a respectful and constructive conversation.

Rest assured that your email address will remain private and will not be published or shared with anyone. We prioritize the privacy and security of our users.

Free Course
Get a free Linux course before you go.
Subscribe to TecMint Weekly and get the Learn Linux 7 Days Crash Course free. Read by 34,000+ Linux professionals every Thursday.
Something went wrong. Please try again.
Check your email for a magic link to get started.