0
0
Bash-scriptingHow-ToBeginner · 2 min read

Bash Script to Print Multiplication Table

Use a Bash script with nested for loops like for i in {1..10}; do for j in {1..10}; do echo "$i x $j = $((i*j))"; done; done to print a multiplication table.
📋

Examples

Input1 to 3
Output1 x 1 = 1 1 x 2 = 2 1 x 3 = 3 2 x 1 = 2 2 x 2 = 4 2 x 3 = 6 3 x 1 = 3 3 x 2 = 6 3 x 3 = 9
Input1 to 5
Output1 x 1 = 1 1 x 2 = 2 1 x 3 = 3 1 x 4 = 4 1 x 5 = 5 2 x 1 = 2 2 x 2 = 4 2 x 3 = 6 2 x 4 = 8 2 x 5 = 10 3 x 1 = 3 3 x 2 = 6 3 x 3 = 9 3 x 4 = 12 3 x 5 = 15 4 x 1 = 4 4 x 2 = 8 4 x 3 = 12 4 x 4 = 16 4 x 5 = 20 5 x 1 = 5 5 x 2 = 10 5 x 3 = 15 5 x 4 = 20 5 x 5 = 25
Input1 to 1
Output1 x 1 = 1
🧠

How to Think About It

To print a multiplication table, think of two numbers: one for rows and one for columns. Use two loops: the outer loop picks the first number, and the inner loop picks the second. Multiply them and print the result for each pair.
📐

Algorithm

1
Set the range of numbers for the table (e.g., 1 to 10).
2
Start the outer loop from the first number to the last number.
3
Inside the outer loop, start the inner loop from the first number to the last number.
4
Multiply the current numbers from both loops.
5
Print the multiplication expression and result.
6
Repeat until all pairs are printed.
💻

Code

bash
#!/bin/bash
for i in {1..10}; do
  for j in {1..10}; do
    echo "$i x $j = $((i * j))"
  done
done
Output
1 x 1 = 1 1 x 2 = 2 1 x 3 = 3 1 x 4 = 4 1 x 5 = 5 1 x 6 = 6 1 x 7 = 7 1 x 8 = 8 1 x 9 = 9 1 x 10 = 10 2 x 1 = 2 2 x 2 = 4 2 x 3 = 6 2 x 4 = 8 2 x 5 = 10 2 x 6 = 12 2 x 7 = 14 2 x 8 = 16 2 x 9 = 18 2 x 10 = 20 3 x 1 = 3 3 x 2 = 6 3 x 3 = 9 3 x 4 = 12 3 x 5 = 15 3 x 6 = 18 3 x 7 = 21 3 x 8 = 24 3 x 9 = 27 3 x 10 = 30 4 x 1 = 4 4 x 2 = 8 4 x 3 = 12 4 x 4 = 16 4 x 5 = 20 4 x 6 = 24 4 x 7 = 28 4 x 8 = 32 4 x 9 = 36 4 x 10 = 40 5 x 1 = 5 5 x 2 = 10 5 x 3 = 15 5 x 4 = 20 5 x 5 = 25 5 x 6 = 30 5 x 7 = 35 5 x 8 = 40 5 x 9 = 45 5 x 10 = 50 6 x 1 = 6 6 x 2 = 12 6 x 3 = 18 6 x 4 = 24 6 x 5 = 30 6 x 6 = 36 6 x 7 = 42 6 x 8 = 48 6 x 9 = 54 6 x 10 = 60 7 x 1 = 7 7 x 2 = 14 7 x 3 = 21 7 x 4 = 28 7 x 5 = 35 7 x 6 = 42 7 x 7 = 49 7 x 8 = 56 7 x 9 = 63 7 x 10 = 70 8 x 1 = 8 8 x 2 = 16 8 x 3 = 24 8 x 4 = 32 8 x 5 = 40 8 x 6 = 48 8 x 7 = 56 8 x 8 = 64 8 x 9 = 72 8 x 10 = 80 9 x 1 = 9 9 x 2 = 18 9 x 3 = 27 9 x 4 = 36 9 x 5 = 45 9 x 6 = 54 9 x 7 = 63 9 x 8 = 72 9 x 9 = 81 9 x 10 = 90 10 x 1 = 10 10 x 2 = 20 10 x 3 = 30 10 x 4 = 40 10 x 5 = 50 10 x 6 = 60 10 x 7 = 70 10 x 8 = 80 10 x 9 = 90 10 x 10 = 100
🔍

Dry Run

Let's trace printing the multiplication table from 1 to 3.

1

Outer loop starts with i=1

Inner loop runs j=1 to 3, printing 1 x 1=1, 1 x 2=2, 1 x 3=3

2

Outer loop i=2

Inner loop runs j=1 to 3, printing 2 x 1=2, 2 x 2=4, 2 x 3=6

3

Outer loop i=3

Inner loop runs j=1 to 3, printing 3 x 1=3, 3 x 2=6, 3 x 3=9

ijOutput
111 x 1 = 1
121 x 2 = 2
131 x 3 = 3
212 x 1 = 2
222 x 2 = 4
232 x 3 = 6
313 x 1 = 3
323 x 2 = 6
333 x 3 = 9
💡

Why This Works

Step 1: Outer loop controls the first number

The for loop with variable i runs through the first number in the multiplication.

Step 2: Inner loop controls the second number

The nested for loop with variable j runs through the second number for each i.

Step 3: Multiplication and printing

Inside the inner loop, $((i * j)) calculates the product, and echo prints the formatted result.

🔄

Alternative Approaches

Using while loops
bash
#!/bin/bash
i=1
while [ $i -le 10 ]; do
  j=1
  while [ $j -le 10 ]; do
    echo "$i x $j = $((i * j))"
    ((j++))
  done
  ((i++))
done
While loops offer more control but are slightly longer and less concise than for loops.
Printing formatted table with tabs
bash
#!/bin/bash
for i in {1..10}; do
  for j in {1..10}; do
    printf "%d\t" $((i * j))
  done
  echo
 done
This prints a clean grid of products without the multiplication expression, useful for compact tables.
Using seq command for range
bash
#!/bin/bash
for i in $(seq 1 10); do
  for j in $(seq 1 10); do
    echo "$i x $j = $((i * j))"
  done
done
Using <code>seq</code> works on systems without brace expansion but is slightly slower.

Complexity: O(n^2) time, O(1) space

Time Complexity

The script uses two nested loops each running n times, so it performs n × n = n² multiplications and prints.

Space Complexity

The script uses a fixed amount of memory for variables and output lines, so space is constant O(1).

Which Approach is Fastest?

Using for loops with brace expansion is fastest and simplest; while loops and seq add slight overhead but offer flexibility.

ApproachTimeSpaceBest For
For loops with brace expansionO(n^2)O(1)Simple and fast on modern Bash
While loopsO(n^2)O(1)More control, slightly longer code
Seq commandO(n^2)O(1)Compatibility on systems without brace expansion
💡
Use nested loops to cover all pairs of numbers for the multiplication table.
⚠️
Beginners often forget to nest loops, printing only one row or column instead of the full table.