0
0
Bash-scriptingHow-ToBeginner · 2 min read

Bash Script to Print Hollow Square Pattern

Use nested loops in Bash: outer loop for rows and inner loop for columns, print '*' for borders and space for inside, e.g., for ((i=1;i<=n;i++)); do for ((j=1;j<=n;j++)); do if [[ $i -eq 1 || $i -eq n || $j -eq 1 || $j -eq n ]]; then echo -n '*'; else echo -n ' '; fi; done; echo; done.
📋

Examples

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

How to Think About It

To print a hollow square, think of a grid with rows and columns. Print stars on the first and last rows and columns to form the border. For all other positions inside, print spaces to keep it hollow.
📐

Algorithm

1
Get the size of the square from the user.
2
Loop through each row from 1 to size.
3
Inside each row, loop through each column from 1 to size.
4
If the current position is on the border (first or last row or column), print '*'.
5
Otherwise, print a space.
6
After each row, print a newline.
💻

Code

bash
#!/bin/bash
read -p "Enter size of square: " n
for ((i=1; i<=n; i++)); do
  for ((j=1; j<=n; j++)); do
    if [[ $i -eq 1 || $i -eq n || $j -eq 1 || $j -eq n ]]; then
      echo -n '*'
    else
      echo -n ' '
    fi
  done
  echo
 done
Output
Enter size of square: 5 ***** * * * * * * *****
🔍

Dry Run

Let's trace the code for input size 3 through the loops.

1

First row (i=1)

j=1: border, print '*'; j=2: border, print '*'; j=3: border, print '*'

2

Second row (i=2)

j=1: border, print '*'; j=2: inside, print ' '; j=3: border, print '*'

3

Third row (i=3)

j=1: border, print '*'; j=2: border, print '*'; j=3: border, print '*'

Row iColumn jPrint
11*
12*
13*
21*
22
23*
31*
32
33*
💡

Why This Works

Step 1: Outer and inner loops

The outer loop runs through each row, and the inner loop runs through each column in that row.

Step 2: Border check

We check if the current position is on the first or last row or column using if conditions.

Step 3: Print star or space

If on border, print *; otherwise, print a space to create the hollow effect.

🔄

Alternative Approaches

Using single loop with arithmetic
bash
#!/bin/bash
read -p "Enter size: " n
for ((i=1; i<=n*n; i++)); do
  row=$(((i-1)/n+1))
  col=$(((i-1)%n+1))
  if [[ $row -eq 1 || $row -eq n || $col -eq 1 || $col -eq n ]]; then
    echo -n '*'
  else
    echo -n ' '
  fi
  if (( i % n == 0 )); then echo; fi
done
Uses one loop and arithmetic to calculate row and column; slightly less readable but compact.
Using printf for formatting
bash
#!/bin/bash
read -p "Enter size: " n
for ((i=1; i<=n; i++)); do
  line=""
  for ((j=1; j<=n; j++)); do
    if [[ $i -eq 1 || $i -eq n || $j -eq 1 || $j -eq n ]]; then
      line+="*"
    else
      line+=" "
    fi
  done
  printf "%s\n" "$line"
done
Builds each line as a string then prints it, which can be easier to modify or extend.

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 iterations, resulting in O(n^2) time.

Space Complexity

The script uses a fixed amount of extra memory regardless of input size, so space complexity is O(1).

Which Approach is Fastest?

All approaches have similar O(n^2) time, but single loop with arithmetic may be slightly faster due to fewer loop overheads, while building lines as strings improves readability.

ApproachTimeSpaceBest For
Nested loopsO(n^2)O(1)Simplicity and clarity
Single loop with arithmeticO(n^2)O(1)Compact code, slight speed gain
Building line stringO(n^2)O(n)Readability and easy modification
💡
Use echo -n to print characters without a newline inside loops.
⚠️
Forgetting to print a newline after each row, causing all output to appear on one line.