02 August 2013

Shell Script to print pyramid of Stars

14 comments

Q. How do I print pyramid of stars using function in Bash


Ans.

bash script to print pyramid of stars
Before writing bash script lets understand how we going to print this pattern. We do it in two part, first we are going to print part 1 and then we print the part 2.

#!/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 part1 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 used for printing new line
      echo;
  done
}

#calling function

#change number according to your need
makePyramid 5


Output


how to print star pyramid in bash script

Note: You can combine loop of Part1 and Part2 to make it more simple

#!/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 part1 and part2 of the the pyramid
      for((j=1;j<=2*i-1;j++))
      do
	echo -ne "*"
      done
      
      #This echo used for printing new line
      echo;
  done
}

#calling function

#change number according to your need
makePyramid 5

Output

how to print star pyramid in bash script

Enjoy :)


If You Liked This Post Please Take a Time To Share This Post

You May Also Like...

14 comments:

  1. Thanks Sandeep, script worked great.

    ReplyDelete
  2. #!/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

    ReplyDelete
    Replies
    1. I have to make a pyramid that asks for a number from 1-9 and if i forexample type 4 it goes
      1
      22
      333
      4444
      Help please :P - See more at: http://technicalworldforyou.blogspot.no/2013/08/shell-script-to-print-pyramid-of-stars.html?showComment=1447173031613#c970679908834552194

      Delete
  3. I have to make a pyramid that asks for a number from 1-9 and if i forexample type 4 it goes
    1
    22
    333
    4444
    Help please :P

    ReplyDelete
  4. An interesting topic it is. I enjoyed reading, and the knowledge and perspective that my reading gives me has strengthened my teaching skills and presentation abilities. Just letting you know how I feel! You always had great informative, down-to-earth, enjoyable, real-life posts!


    Resume writer @ professional resume writing service

    ReplyDelete
  5. Can anyone please tell me that how could we make diamond shape with stars in linux bin bash scripting?
    By using 3 or 6 loops?

    ReplyDelete
  6. I have just won a unlimited psn codes unused or can say gift card codes after playing a games on the psn code generator

    ReplyDelete
  7. How To print This in unix shell script
    *
    * *
    * *
    * *



    ReplyDelete
  8. #!/bin/bash

    read -p "N? : " 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

    To Print:
    1
    22
    333
    4444

    ReplyDelete
  9. How can I make The output like. 1
    2 3
    4 5 6
    7 8 9 10
    Please quickly because the last time for assignments is tomorrow

    ReplyDelete
    Replies
    1. s=0
      for(( i=1;i<=4;i++ ))
      do
      for((j=1;j<=4;j++))
      do
      a=$(($s+1))
      echo -n "$a"

      Delete