• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar
  • Skip to footer navigation

The Geek Diary

  • OS
    • Linux
    • CentOS/RHEL
    • Solaris
    • Oracle Linux
    • VCS
  • Interview Questions
  • Database
    • oracle
    • oracle 12c
    • ASM
    • mysql
    • MariaDB
  • DevOps
    • Docker
    • Shell Scripting
  • Big Data
    • Hadoop
    • Cloudera
    • Hortonworks HDP

Shell Script to print pyramid of Stars

by admin

The Basics

So here we will print the pyramid of stars in two parts as shown below. We will loop through the number provided by the user and print the first half of the stars using a for loop and other half using another for loop. The spaces and new line characters are added in a different section.

pyramid of stars in shell script

The Script

1. Edit the file /tmp/star_pyramid.sh and add the below script into it:

#!/bin/bash
makePyramid()
{
  # Here $1 is the parameter you passed with the function i,e 5
  n=$1;

  # outer loop is for printing number of rows in the pyramid
  for((i=1;i<=n;i++))
  do

      # This loop print spaces required
      for((k=i;k<=n;k++))
      do
        echo -ne " ";
      done

      # This loop print part 1 of the the pyramid
      for((j=1;j<=i;j++))
      do
      echo -ne "*";
      done

      # This loop print part 2 of the pryamid.
      for((z=1;z<i;z++))
      do
      echo -ne "*";
      done
      
      # This echo is used for printing a new line
      echo;
  done
}

# calling function
# Pass the number of levels you need in the parameter while running the script.
makePyramid $1

2. Provide executible permissions on the script.

# chmod +x /tmp/star_pyramid.sh

3. While running the script provide the number of levels you want in the output. For example:

$ /tmp/star_pyramid.sh 10
          *
         ***
        *****
       *******
      *********
     ***********
    *************
   ***************
  *****************
 *******************

Another way

Here is another way to print the pyramid of stars using shell script.

#!/bin/bash

clear
echo -n "Enter the level of pyramid: "; read n

star=''
space=''

for ((i=0; i<n; i++ ))
do
space="$space "
done

echo "$space|"

for (( i=1; i<n; i++ ))
do

star="$star*"
space="${space%?}"
echo "$space$star|$star";

done

Make the script executable and run it.

$ /tmp/star_pyramid.sh
Enter the level of pyramid: 10
          |
         *|*
        **|**
       ***|***
      ****|****
     *****|*****
    ******|******
   *******|*******
  ********|********
 *********|*********

Pyramid of Numbers using shell script

Similar to the above 2 examples, you may also print a pyramid of numbers using the below script.

#!/bin/bash

read -p "How many levels? : " n

for((i = 0; i < n; i++))
do
k=0
while((k < $((i+1))))
do
echo -e "$((i+1))\c"
k=$((k+1))
done

echo " "
done

Make the script executable and run it.

$ /tmp/star_pyramid.sh
How many levels? : 5
1
22
333
4444
55555

Filed Under: DevOps, Linux, Shell Scripting

Some more articles you might also be interested in …

  1. How to disable SSH host key checking in Linux
  2. Wallch (Wallpaper Changer) – Rotate Ubuntu Desktop Wallpapers
  3. How to use ldconfig Command in Linux
  4. Grsync: Graphical rsync backup tool in Ubuntu Linux
  5. conky Command Examples in Linux
  6. aptitude: command not found
  7. How to Lock and Unlock Zimbra Accounts from Command Line
  8. “btrfs” command examples to Create and Manage Btrfs File System
  9. service Command Examples in Linux
  10. gpg: command not found

You May Also Like

Primary Sidebar

Recent Posts

  • ncat Command Examples in Linux
  • ncat: command not found
  • nautilus Command Examples in Linux
  • namei: command not found

© 2023 · The Geek Diary

  • Archives
  • Contact Us
  • Copyright