write a shell script to display the multiplication table of any number

Write a Shell Script to Display Multiplication Table of Any Number Easily

By David King
AD

Ever found yourself needing to quickly generate a multiplication table for a specific number? While there are numerous online tools and calculators available, sometimes you might want to create a simple, reusable script to accomplish this task. This is where shell scripting comes in handy. In this guide, we'll walk you through creating a shell script that displays the multiplication table of any number you input.

Before we dive into the script, let's ensure you have a basic understanding of shell scripting. Shell scripts are plain text files that contain a sequence of commands for a Unix-based operating system to execute. They are written in a scripting language built into the shell, which is a command-line interpreter or terminal. In this case, we'll be using Bash, a popular shell on Linux and macOS.

Setting Up the Shell Script

To create a shell script, you'll first need to open a text editor. For this guide, we'll use nano, a simple and widely available text editor. In your terminal, type:

AD
Table of 23 - Multiplication Tables
Table of 23 - Multiplication Tables
Multiplication Tables (2, 3, 4) worksheet
Multiplication Tables (2, 3, 4) worksheet
Tablas de multiplicar
Tablas de multiplicar
```bash nano multiplication_table.sh ```

This will open a new file named 'multiplication_table.sh' in the nano text editor.

Accepting User Input

Our script will start by accepting user input for the number whose multiplication table we want to generate. To do this, we'll use the `read` command in Bash. Add the following line to your script:

two times tables with numbers and the same number
two times tables with numbers and the same number
Multiplication Tables and Charts
Multiplication Tables and Charts
Multiplication Tables - 21 to 30
Multiplication Tables - 21 to 30
```bash echo "Enter a number: " read num ```

This line will display the prompt "Enter a number: " and store the user's input in the variable 'num'.

Validating User Input

Before generating the multiplication table, we should validate that the user has entered a positive integer. Add the following lines to your script:

AD
```bash re='^[0-9]+$' if ! [[ $num =~ $re ]] ; then echo "Error: Not a number" >&2; exit 1 fi if [ $num -lt 1 ]; then echo "Error: Number must be positive" >&2; exit 1 fi ```

These lines use regular expressions to check if the input is a number, and then ensure it's a positive integer. If the input is invalid, the script will display an error message and exit.

a poster with numbers on it that says,'multiplications'in black and white
a poster with numbers on it that says,'multiplications'in black and white
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|
Tables 1 to 12 – Multiplication Tables 1 to 12 » Onlymyenglish.com
Tables 1 to 12 – Multiplication Tables 1 to 12 » Onlymyenglish.com
18 Printable Multiplication Charts (Free PDF Download)
18 Printable Multiplication Charts (Free PDF Download)
Multiplication Strategies
Multiplication Strategies
Free Printable Multiplication Table 1-100 Chart
Free Printable Multiplication Table 1-100 Chart

Generating the Multiplication Table

Now that we have a valid number, we can generate the multiplication table. We'll use a for loop to iterate through numbers 1 to 10 and calculate their product with the user's input. Add the following lines to your script:

```bash echo "Multiplication table for $num:" for i in {1..10} do echo "$num * $i = $((num*i))" done ```

This will display the multiplication table for the input number, with each line showing the product of the number and the current iteration of the loop.

AD

Making the Script Executable

Before you can run your script, you need to make it executable. In your terminal, type:

multiplication tables Archives - Study Hut Tutoring
multiplication tables Archives - Study Hut Tutoring
Multiplication Chart
Multiplication Chart
Free Multiplication Table Printables for 3rd Grade & 4th Grade Math
Free Multiplication Table Printables for 3rd Grade & 4th Grade Math
2 Digit by 2 Digit Multiplication Anchor Chart Reference Multiplication Posters
2 Digit by 2 Digit Multiplication Anchor Chart Reference Multiplication Posters
Division Rules Anchor Chart, Multiplication And Division Anchor Chart 5th Grade, Multiplication And Division Anchor Chart 4th Grade, Multi Digit Division Anchor Chart, Multiples Anchor Chart 4th Grade, 4th Grade Division Anchor Chart, Dividing Multi Digit Numbers Anchor Chart, 5th Grade Math T-chart, Daily 5 Anchor Charts Pdf
Division Rules Anchor Chart, Multiplication And Division Anchor Chart 5th Grade, Multiplication And Division Anchor Chart 4th Grade, Multi Digit Division Anchor Chart, Multiples Anchor Chart 4th Grade, 4th Grade Division Anchor Chart, Dividing Multi Digit Numbers Anchor Chart, 5th Grade Math T-chart, Daily 5 Anchor Charts Pdf
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
Multiplication Chart 1 to 15 table for Kids [Free Printable ]
Multiplication Chart 1 to 15 table for Kids [Free Printable ]
a piece of paper that has some writing on it with arrows pointing to different numbers
a piece of paper that has some writing on it with arrows pointing to different numbers
Engaging Hands-On Multiplication Activities for Class 1: Mastering By 2s and 4s
Engaging Hands-On Multiplication Activities for Class 1: Mastering By 2s and 4s
Multiplication Tables (5, 6, 7) worksheet
Multiplication Tables (5, 6, 7) worksheet
70+ Fun Multiplication Worksheets ⭐ Charts, Flash Cards
70+ Fun Multiplication Worksheets ⭐ Charts, Flash Cards
Fun Multiplication Worksheet for Kids | Learn Math Basics
Fun Multiplication Worksheet for Kids | Learn Math Basics
Printable Multiplication Table, Multiplication Chart, and Multiplication Worksheets .
Printable Multiplication Table, Multiplication Chart, and Multiplication Worksheets .
Engaging Box Method Multiplication Anchor Chart for 4th Grade Math
Engaging Box Method Multiplication Anchor Chart for 4th Grade Math
```bash chmod +x multiplication_table.sh ```

Now you can run your script with:

```bash ./multiplication_table.sh ```

This will display the prompt "Enter a number: ", and after you input a number, it will generate and display the multiplication table for that number.

AD

And there you have it! You've created a simple shell script that generates multiplication tables. This script is a great example of how shell scripting can automate repetitive tasks, making your work more efficient. Happy scripting!

AD