shell script to display multiplication table of given number

Shell Script to Display Multiplication Table of Given Number - Easy Code Example

By Rachel Clark
AD

Ever needed to display a multiplication table for a specific number? Instead of manually calculating it, you can create a shell script to do the heavy lifting. This not only saves time but also ensures accuracy. Let's explore how to create a shell script for this task.

Before we dive into the script, let's ensure you're familiar with basic shell scripting concepts. If not, don't worry, we'll keep it beginner-friendly.

Understanding Shell Scripting

Shell scripting is a way to automate tasks on Unix-based systems (like Linux or macOS). It's a powerful tool for developers and system administrators alike. The shell script we'll create will be written in Bash, the default shell on most Linux systems.

AD
Multiplication Tables - Shaykh Academy
Multiplication Tables - Shaykh Academy
a table that has numbers on it and the words'multiplication tables '
a table that has numbers on it and the words'multiplication tables '
Multiplication Tables (2, 3, 4) worksheet
Multiplication Tables (2, 3, 4) worksheet

Bash scripts are plain text files that contain a sequence of commands. These commands are executed in the order they appear in the file. To create a script, you'll need a text editor like nano, vim, or emacs.

Bash Script Basics

Here are some basic Bash commands you'll need to understand:

Table of 23 - Multiplication Tables
Table of 23 - Multiplication Tables
the power table from 1 to 10 is shown with numbers in green and white letters
the power table from 1 to 10 is shown with numbers in green and white letters
the seven times tables worksheet for kids to learn how to multiply and multiply
the seven times tables worksheet for kids to learn how to multiply and multiply
  • echo: Displays a line of text
  • read: Reads a line from the user
  • for: Executes a block of code for each item in a list
  • while: Executes a block of code while a condition is true

Running a Bash Script

To run a Bash script, you need to make it executable. You can do this with the chmod command. For example, to make a file named script.sh executable, you would run:

chmod +x script.sh

AD

Then, you can run the script with ./, like so: ./script.sh

Multiplication Chart 1 to 15 table for Kids [Free Printable ]
Multiplication Chart 1 to 15 table for Kids [Free Printable ]
Multiplication Strategies Poster/Cheat Sheet
Multiplication Strategies Poster/Cheat Sheet
Tablas de multiplicar
Tablas de multiplicar
Multiplication Strategies Math Anchor Chart 2
Multiplication Strategies Math Anchor Chart 2
Multiplication Table | Learn Tables from 0 – 25 | Multiplication Table
Multiplication Table | Learn Tables from 0 – 25 | Multiplication Table
18 Printable Multiplication Charts (Free PDF Download)
18 Printable Multiplication Charts (Free PDF Download)

Creating the Multiplication Table Script

Now that we've covered the basics, let's create our multiplication table script. We'll use a loop to iterate through the numbers and display the results.

Here's a simple script that does this:

AD
#!/bin/bash

# Prompt the user for a number
read -p "Enter a number: " num

# Check if the input is a number
re='^[0-9]+$'
if ! [[ $num =~ $re ]] ; then
   echo "Error: Not a number" >&2; exit 1
fi

# Print the multiplication table
echo "Multiplication table for $num:"
for i in {1..10}
do
   echo "$num * $i = $((num*i))"
done

Explanation of the Script

The script starts by prompting the user to enter a number. It then checks if the input is a valid number. If it's not, the script exits with an error message.

2 Digit by 2 Digit Multiplication Anchor Chart Reference Multiplication Posters
2 Digit by 2 Digit Multiplication Anchor Chart Reference Multiplication Posters
a poster showing the numbers for students to learn
a poster showing the numbers for students to learn
Free Multiplication Table Printables for 3rd Grade & 4th Grade Math
Free Multiplication Table Printables for 3rd Grade & 4th Grade Math
Multiplication Number Posters for Engaging Math Practice | Printable Times Table Charts
Multiplication Number Posters for Engaging Math Practice | Printable Times Table Charts
Multiplication Tables and Charts
Multiplication Tables and Charts
14K views · 752 reactions | Multiplication table 1 and 2 chart | fun with maths #multiple #tables #maths #shorts #shortreels #viralreelsfb #familyday | Anas Creativity | Facebook
14K views · 752 reactions | Multiplication table 1 and 2 chart | fun with maths #multiple #tables #maths #shorts #shortreels #viralreelsfb #familyday | Anas Creativity | Facebook
a table with numbers and times on it
a table with numbers and times on it
Multiplication Tables - 21 to 30
Multiplication Tables - 21 to 30
Colorful Multiplication Table of 2 & 4 for Kids | Fun Learning Activity
Colorful Multiplication Table of 2 & 4 for Kids | Fun Learning Activity
Free Multiplication Chart 1-100 Table [Printable PDF]
Free Multiplication Chart 1-100 Table [Printable PDF]
Multiplications | skip counting | time tables | classroom maths display decor maths 1-12 class modern display for maths arithmetic
Multiplications | skip counting | time tables | classroom maths display decor maths 1-12 class modern display for maths arithmetic
Multiplication Tables TLM |Math working model |math tlm for class 1|Maths Tables|
Multiplication Tables TLM |Math working model |math tlm for class 1|Maths Tables|
Google Image Result for https://purpleturtle.us/cdn/shop/products/MULTIPLICATION_2048x.jpg?v=1645182437
Google Image Result for https://purpleturtle.us/cdn/shop/products/MULTIPLICATION_2048x.jpg?v=1645182437
Multiplication Chart
Multiplication Chart

The script then prints a header for the multiplication table. It uses a for loop to iterate through the numbers 1 to 10. For each number, it calculates the product with the user's input and displays the result.

Running the Script

To run the script, save it to a file (e.g., multiply.sh), make it executable with chmod +x multiply.sh, and then run it with ./multiply.sh. When prompted, enter a number, and the script will display the multiplication table for that number.

AD

And there you have it! You've just created a shell script to display a multiplication table. This script is a great example of how shell scripting can automate simple tasks, saving you time and effort.

Now that you've mastered this, why not explore more complex scripts? The possibilities are endless. Happy scripting!

AD