0
0
Bash-scriptingHow-ToBeginner · 2 min read

Bash Script to Print Diamond Pattern with Stars

Use a Bash script with nested loops to print spaces and stars in increasing and then decreasing order, like: for ((i=1; i<=n; i++)); do for ((j=n-i; j>0; j--)); do echo -n ' '; done; for ((k=1; k<=2*i-1; k++)); do echo -n '*'; done; echo; done and then reverse for the bottom half.
📋

Examples

Input3
Output * *** ***** *** *
Input5
Output * *** ***** ******* ********* ******* ***** *** *
Input1
Output*
🧠

How to Think About It

To print a diamond pattern, first print the top half by increasing the number of stars each line while decreasing spaces before them. Then print the bottom half by decreasing stars and increasing spaces. This creates a symmetrical diamond shape.
📐

Algorithm

1
Get input number n for diamond size
2
For i from 1 to n, print (n - i) spaces and (2*i - 1) stars
3
For i from n-1 down to 1, print (n - i) spaces and (2*i - 1) stars
4
End
💻

Code

bash
#!/bin/bash
read -p "Enter diamond size: " n

for ((i=1; i<=n; i++))
do
  for ((j=n-i; j>0; j--))
  do
    echo -n " "
  done
  for ((k=1; k<=2*i-1; k++))
  do
    echo -n "*"
  done
  echo
 done

for ((i=n-1; i>=1; i--))
do
  for ((j=n-i; j>0; j--))
  do
    echo -n " "
  done
  for ((k=1; k<=2*i-1; k++))
  do
    echo -n "*"
  done
  echo
 done
Output
* *** ***** *** *
🔍

Dry Run

Let's trace the diamond pattern for input 3 through the code

1

Top half line 1

i=1, spaces=2, stars=1, prints ' *'

2

Top half line 2

i=2, spaces=1, stars=3, prints ' ***'

3

Top half line 3

i=3, spaces=0, stars=5, prints '*****'

4

Bottom half line 1

i=2, spaces=1, stars=3, prints ' ***'

5

Bottom half line 2

i=1, spaces=2, stars=1, prints ' *'

LineSpacesStarsOutput
121 *
213 ***
305*****
413 ***
521 *
💡

Why This Works

Step 1: Print spaces before stars

The code prints n - i spaces to align stars in the center for each line.

Step 2: Print stars in odd counts

It prints 2*i - 1 stars to form the increasing and then decreasing width of the diamond.

Step 3: Top and bottom halves

The first loop prints the top half, the second loop prints the bottom half in reverse order to complete the diamond.

🔄

Alternative Approaches

Using printf for formatting
bash
#!/bin/bash
read -p "Enter diamond size: " n
for ((i=1; i<=n; i++)); do
  printf "%*s" $((n - i)) ""
  printf "%0.s*" $(seq 1 $((2*i-1)))
  echo
 done
for ((i=n-1; i>=1; i--)); do
  printf "%*s" $((n - i)) ""
  printf "%0.s*" $(seq 1 $((2*i-1)))
  echo
 done
This uses printf for cleaner spacing and star printing, improving readability.
Single loop with conditional
bash
#!/bin/bash
read -p "Enter diamond size: " n
for ((i=1; i<2*n; i++)); do
  if (( i <= n )); then
    spaces=$((n - i))
    stars=$((2*i - 1))
  else
    spaces=$((i - n))
    stars=$((4*n - 2*i - 1))
  fi
  for ((j=0; j<spaces; j++)); do echo -n " "; done
  for ((k=0; k<stars; k++)); do echo -n "*"; done
  echo
 done
This uses one loop and conditions to handle both halves, reducing code repetition.

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

Time Complexity

The script uses nested loops where the inner loops run proportional to the line number, resulting in O(n^2) time.

Space Complexity

The script uses constant extra space for counters and prints directly, so space complexity is O(1).

Which Approach is Fastest?

All approaches have similar O(n^2) time, but using printf can be more efficient and cleaner than multiple echo calls.

ApproachTimeSpaceBest For
Nested loops with echoO(n^2)O(1)Simple and clear for beginners
Using printfO(n^2)O(1)Cleaner output and formatting
Single loop with conditionO(n^2)O(1)Less code repetition, compact logic
💡
Use nested loops: outer for lines, inner for spaces and stars to shape the diamond.
⚠️
Beginners often forget to print the bottom half in reverse order, resulting in a triangle instead of a diamond.