0
0
Bash-scriptingHow-ToBeginner · 2 min read

Bash Script to Print Inverted Pyramid Pattern

Use a nested loop in Bash where the outer loop controls the rows and the inner loops print spaces and stars; for example, for ((i=n; i>=1; i--)); do printf "%*s" $((n-i)) ""; for ((j=1; j<=2*i-1; j++)); do printf "*"; done; echo; done prints an inverted pyramid of stars.
📋

Examples

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

How to Think About It

To print an inverted pyramid, start from the widest row with the most stars and reduce the number of stars each row while increasing the leading spaces. Use one loop to count down the rows and inside it, print spaces first, then stars to form the shape.
📐

Algorithm

1
Get the number of rows (n) from the user.
2
For each row from n down to 1:
3
Print (n - current row) spaces to indent the stars.
4
Print (2 * current row - 1) stars to form the pyramid row.
5
Move to the next line after printing stars.
💻

Code

bash
read -p "Enter number of rows: " n
for ((i=n; i>=1; i--)); do
  printf "%*s" $((n - i)) ""
  for ((j=1; j<=2*i-1; j++)); do
    printf "*"
  done
  echo
 done
Output
Enter number of rows: 5 ********* ******* ***** *** *
🔍

Dry Run

Let's trace the input 3 through the code

1

First row (i=3)

Print 0 spaces, then 5 stars: *****

2

Second row (i=2)

Print 1 space, then 3 stars: ***

3

Third row (i=1)

Print 2 spaces, then 1 star: *

Row (i)SpacesStars
305
213
121
💡

Why This Works

Step 1: Outer loop controls rows

The outer loop counts down from n to 1, controlling how many rows the pyramid has.

Step 2: Print spaces for indentation

For each row, print spaces equal to (n - current row) to shift stars right and create the pyramid shape.

Step 3: Print stars for pyramid

Print stars equal to (2 * current row - 1) to form the width of the pyramid row.

🔄

Alternative Approaches

Using single printf with format string
bash
read -p "Enter rows: " n
for ((i=n; i>=1; i--)); do
  printf "%*s%*s\n" $((n - i)) "" $((2*i - 1)) "$(printf '*%.0s' $(seq 1 $((2*i - 1))))"
done
This method uses printf with formatted strings and sequence expansion but is less readable.
Using while loops
bash
read -p "Enter rows: " n
i=$n
while [ $i -ge 1 ]; do
  spaces=$((n - i))
  stars=$((2*i - 1))
  j=0
  while [ $j -lt $spaces ]; do printf " "; j=$((j+1)); done
  j=0
  while [ $j -lt $stars ]; do printf "*"; j=$((j+1)); done
  echo
  i=$((i-1))
done
Using while loops is more verbose but may be easier for beginners to understand.

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

Time Complexity

The script uses nested loops: the outer loop runs n times and the inner loop runs up to 2n-1 times, resulting in O(n^2) time.

Space Complexity

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

Which Approach is Fastest?

All approaches have similar time complexity; using printf with sequences may be slightly slower but more concise.

ApproachTimeSpaceBest For
Nested for loopsO(n^2)O(1)Clarity and simplicity
Single printf with sequenceO(n^2)O(1)Conciseness, less readable
While loopsO(n^2)O(1)Beginners learning loops
💡
Use nested loops: outer for rows, inner for spaces and stars to shape the pyramid.
⚠️
Forgetting to increase spaces each row causes the pyramid to not align properly.