Printing pyramid with stars in Unix Shell Programming

The below unix shell programming will show you an example on printing pyramid with stars in unix shell programming. This tutorial will also show how to use if else condition, how to use for loop in shell programming. You will also see how to take input from keyboard.

I will be printing pyramid with stars in unix shell programming in two ways – one is stars exist inside and another one is stars do not exist inside the pyramid.

So let’s see the first example where stars exist also inside the pyramid.

The below shell program first takes the depth or level or height from the user for the pyramid. The program itself calculates the number of stars at each level according to the height of the pyramid. I also put the space between two stars to look good.

The below shell programming is written into the file pyramid-stars-inner.sh.

#!/bin/bash

read -r -p "Enter depth of pyramid: " n
echo "You enetered level: $n"

space=n

for((i=1;i<=n;i++))
do
        space=$((space-1)) #calculate how many spaces should be printed before *

        for((j=1;j<=space;j++))
        do
                echo -n " " #print spaces on the same line before printing *
        done
        for((k=1;k<=i;k++))
        do
                echo -n "*" #print * on the same line
                echo -n " " #print space after * on the same line
        done
        echo -e #print new line after each row
done

Make sure you change the permission of the above shell script by using the following command:

$ chmod +x pyramid-stars-inner.sh

To run or execute the above shell script use the following command:

$ ./pyramid-stars-inner.sh

After running the above shell program you will get the below output. So here you see that the pyramid is filled with stars in whole area. So this is a solid pyramid where inner surface is also filled with stars.

pyramid print using unix shell programming

Now let’s see the example where outer surface of the pyramid is filled with stars. Here also you will get the input for level or height of the pyramid and accordingly we put stars on outer surface at each level at two furthest ends.

The following shell program is written into the file pyramid-stars-outer.sh.

read -r -p "Enter depth of pyramid: " n
echo "You enetered level: $n"

s=0
space=n

for((i=1;i<=n;i++))
do
	space=$((space-1)) #calculate how many spaces should be printed before *               

	for((j=1;j<=space;j++))
	do
		echo -n " " #print spaces on the same line before printing *
	done
	for((k=1;k<=i;k++))
	do
		if((i==1 || i==2 || i==n))
		then
			echo -n "*" #print * on the same line
			echo -n " " #print space after * on the same line
		elif((k==1))
		then
			echo -n "*" #print * on the same line
		elif((k==i))
		then
			for((l=1;l<=k+s;l++))
			do
							echo -n " " #print spaces on the same line before printing *
			done
			s=$((s+1)) #as pyramid expands at bottom, so we need to recalculate inner spaces
			echo -n "*" #print * on the same line
		fi
	done
	echo -e #print new line after each row
done

Make sure you again change the permission using the command:

$ chmod +x pyramid-stars-outer.sh

To run the shell script use the following command:

$ ./pyramid-stars-outer.sh

Here is the below output you get after running the above shell script.

pyramid print using unix shell programming

Source Code

Download

Leave a Reply

Your email address will not be published.