0
0
Bash-scriptingHow-ToBeginner · 2 min read

Bash Script to Print Half Diamond Pattern

Use nested loops in Bash: first loop from 1 to n printing stars, then loop from n-1 down to 1 printing stars, like for ((i=1; i<=n; i++)); do for ((j=1; j<=i; j++)); do echo -n "*"; done; echo; done; for ((i=n-1; i>=1; i--)); do for ((j=1; j<=i; j++)); do echo -n "*"; done; echo; done.
📋

Examples

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

How to Think About It

To print a half diamond pattern, first print increasing lines of stars from 1 up to the input number, then print decreasing lines from one less than the input down to 1. Each line prints stars equal to the current line number.
📐

Algorithm

1
Get input number n
2
For i from 1 to n, print i stars on a line
3
For i from n-1 down to 1, print i stars on a line
4
End
💻

Code

bash
#!/bin/bash
read -p "Enter number of rows: " n
for ((i=1; i<=n; i++))
do
  for ((j=1; j<=i; j++))
  do
    echo -n "*"
  done
  echo
 done
for ((i=n-1; i>=1; i--))
do
  for ((j=1; j<=i; j++))
  do
    echo -n "*"
  done
  echo
 done
Output
* ** *** ** *
🔍

Dry Run

Let's trace input 3 through the code

1

First loop increasing stars

i=1: print 1 star '*' i=2: print 2 stars '**' i=3: print 3 stars '***'

2

Second loop decreasing stars

i=2: print 2 stars '**' i=1: print 1 star '*'

iStars printed
1*
2**
3***
2**
1*
💡

Why This Works

Step 1: Increasing stars loop

The first loop runs from 1 to n, printing stars equal to the current line number using nested loops and echo -n "*" to stay on the same line.

Step 2: Decreasing stars loop

The second loop runs from n-1 down to 1, printing stars in decreasing order to complete the half diamond shape.

Step 3: Line breaks

After printing stars on each line, echo prints a newline to move to the next line.

🔄

Alternative Approaches

Using single loop with condition
bash
#!/bin/bash
read -p "Enter number of rows: " n
for ((i=1; i<2*n; i++))
do
  if (( i <= n )); then
    stars=$i
  else
    stars=$((2*n - i))
  fi
  for ((j=1; j<=stars; j++))
  do
    echo -n "*"
  done
  echo
 done
This uses one loop and a condition to decide star count, making code shorter but slightly less intuitive.
Using while loops
bash
#!/bin/bash
read -p "Enter number of rows: " n
i=1
while [ $i -le $n ]
do
  j=1
  while [ $j -le $i ]
  do
    echo -n "*"
    ((j++))
  done
  echo
  ((i++))
done
i=$((n-1))
while [ $i -ge 1 ]
do
  j=1
  while [ $j -le $i ]
  do
    echo -n "*"
    ((j++))
  done
  echo
  ((i--))
done
Using while loops instead of for loops is an alternative style, useful for beginners familiar with while.

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

Time Complexity

The script uses nested loops: the outer loop runs up to n times and the inner loop runs up to i times, resulting in roughly n*(n+1)/2 operations, which is O(n^2).

Space Complexity

The script uses a fixed number of variables and prints output directly, so space complexity is O(1).

Which Approach is Fastest?

All approaches have similar time complexity; using a single loop with condition reduces code length but not time complexity.

ApproachTimeSpaceBest For
Two nested for loopsO(n^2)O(1)Clarity and simplicity
Single loop with conditionO(n^2)O(1)Shorter code, moderate clarity
While loopsO(n^2)O(1)Beginners familiar with while
💡
Use nested loops with echo -n to print stars on the same line before printing a newline.
⚠️
Beginners often forget to print a newline after each line, causing all stars to appear on one line.