02 August 2013

Shell Script to print pyramid of Stars

81 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...

81 comments:

  1. #!/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
  2. 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
  3. 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
  4. 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
  5. 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
  6. How To print This in unix shell script
    *
    * *
    * *
    * *



    ReplyDelete
  7. #!/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
  8. Replies
    1. 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

      Delete
  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
  10. * * * * * * * * * * * * * * * * *
    * * * * * * * * * * * * * *
    * * * * * * * * * *
    * * * * * *
    * * * * * * * * *
    * * * * * * *
    * * * * *
    * * *
    *

    ReplyDelete
    Replies
    1. can u please share a code with me ?

      Delete
  11. how to display this pattern can someone pls tell!!!!
    * * * * *
    * * * *
    * * *
    * *
    *

    ReplyDelete
  12. OffshoreDedi offers high quality DMCA Ignored Hosting. We make offshore hosting simple for you to use with reliable offshore servers and one-click installers. Your data is safe with us as it’s is kept in a high security facility in an offshore jurisdiction assuring your privacy.

    ReplyDelete
  13. Thank you for sharing this informative post.My Assignment help is giving assignment help to students.we are already trusted by thousands of students who struggle to write their academic papers and also by those students who simply want Essay Homework Help to save their time and make life easy.

    ReplyDelete
  14. Very Nice Blog! This Blog is Totally Informative About Assignment Help Melbourne In Australia. If You Are Looking For Assignment Help Melbourne Provider Then You Should Definitely Go For Myassignmenthelpau.

    ReplyDelete
  15. He viewed the shape of the sun as being like a cartwheel that has a hollow rim that is full of fire. ebook writing service

    ReplyDelete
  16. ABC Assignment help is a reliable assignment help services holding a dedicated and resourceful team of more than 3000 assignment and essay writers helping students with proficient academic support. Our team of subject experts are available to serve students with well-researched write-ups to score high.

    Read more - social science assignment help

    ReplyDelete
  17. If you are looking for the Software Engineering Help assignment then in this case you can opt for our Essay Writing.we provide the best online assignment help.We also provide law essay help for students across the globe. for more information contact us +16692714848.


    ReplyDelete
  18. writing when you wake up - yeah, that's important. So is writing by hand, rather than keyboard.
    Globex Outreach

    ReplyDelete
  19. The global Two Wheeler Slipper Clutch Market owing to consumers are highly showing shifting their interest towards racing bikes. Western Europe is projected to be the second largest market for two-wheeler slipper clutch owing to increasing production of racing prototype bikes and road legal high performance bikes over the forecast period. China and Japan in the Asia Pacific region are projected to grow with addressable growth rate throughout the forecast period due to continuously increasing disposable income of the middle class population and noteworthy growth in bike racing activity.

    ReplyDelete
  20. Thank you for sharing such a great article. A DNP project is the umbrella term used to describe a scholarly project with the express purpose of translating evidence into practice. You may also hear it referred to as a final or research DNP project. Your DNP project will reflect your specialization/area of interest, allowing you to delve deep and create a project focused on clinical practice. You will use your DNP project to demonstrate mastery of your advanced nursing specialty. Read more on DNP Capstone Project Writers

    ReplyDelete
  21. Many blogs like this cover subjects that just aren’t covered by magazines.
    offshoreservers.net

    ReplyDelete
  22. We at Acadecraft provides different types of Blended Learning Services which include station rotation blended learning, lab rotation blended learning, remote blended learning, flex blended learning, flipped classroom blended learning, project-based blended learning, self-directed blended learning, and supplemental blended learning. Blended learning Solutions

    ReplyDelete
  23. Everyone looking at some information ideas forms the different platform, If you shared some creative and informative ideas then the user is very loyal to read your blog. thanks for share highly valuable article with us. Geometry Solution Manuals

    ReplyDelete
  24. Procurement Resource Presents The Production Cost Of Chlorine Gas (Cl2) In Its New Report


    The new report by Procurement Resource, a global procurement research and consulting firm, looks in-depth into the costs involved in the production of chlorine gas (Cl2). The comprehensive report analyses the production cost of the material, covering raw material costs and co-product credit, equipment costs, land and site costs, labour wages, maintenance costs, financing charges, and the depreciation cost. The extensive study describes the step-wise consumption of material and utilities, along with a detailed process flow diagram. The report also assesses the latest developments within the chlorine gas (Cl2) industry that might influence the costs of production, looking into the capacity expansions, plant turnarounds, and mergers, acquisitions, and investments.

    Download a free sample of the production cost of Chlorine Gas (Cl2) from electrolysis and many more

    ReplyDelete
  25. To introduce the new electric Sonic mixer at a price of $200 in March 2017 then subsequently increase it to $ 250 in April 2017.essay writing service

    ReplyDelete
  26. Very good information you sheared here. For me this info is very useful. I recommend you to visit my site that offers you a free online typing speed test. So Check your typing speed free now!

    ReplyDelete
  27. I always come back to your site to gain more insight. If you are about to complete your degree course, the odds are high that you will need to fulfill a capstone project. The aim of this assignment is to assess the knowledge and skills, which you acquired during the course. But before starting looking for nursing capstone project essay ideas, you need to understand the essence of this assignment. Get Help with Capstone Project
    .

    ReplyDelete
  28. Extremely well written paper, however a few more points can be added to make it all the more resourceful.
    Really great article. Keep it up. Visit Here my profile wheel-generator

    ReplyDelete
  29. Your work is very good and I appreciate you and hopping for some more informative posts. Word Per Minute Typing Speed is going to be the first and biggest benefit. Touch typists can easily reach typing speeds of more than 75-80 words per minute, while hunt and peak typists press hard to reach 30 words per minute. Visit my article for more info.

    ReplyDelete
  30. Nice Blog !
    QuickBooks is an accounting software; however, it isn't immune from technical bugs like QuickBooks Error 30159.Our experts are always available to give you quick service.

    ReplyDelete
  31. Hello, I really enjoyed reading your post. It's very helpful and interesting. Have a look at our blog on Need a sleeping bag in the summer camping tents? Depending on where you sleep, sometimes summer nights are too cold to sleep without a sleeping bag. Hypothermia can affect you at temperatures around 50 degrees Fahrenheit or high temperatures in wet conditions. If you are thinking of going without a sleeping bag, there are alternative options to consider.

    ReplyDelete
  32. Your web-site is very cool. I’m impressed by the details that you have on this site. Holding an online meeting or sharing information with your partners in a video conference can save you money and you will need a good webcam 7 essentials to shoot a great video at home You can check your webcam by going to.

    ReplyDelete
  33. I like your work. You have a lot of really interesting topics on your website. This is very useful to me. I have a blog entitled best camping gear. Check out the camping gear guide for tips on how to choose the best bag for you.

    ReplyDelete
  34. This site is amazing, all the information is very valuable. This is an excellent article. All ideas are like that. I appreciate your post!
    Please refer to the link provided if you need help with spacebar clicker. You can learn about Importance of spacebar test from this article. Feel free to visit and share your thoughts.

    ReplyDelete
  35. Hello , I discovered Your Blog is Amazing . As A substance Writter You Explained Very Well In this . I took in alsot From Your Website . I Read Your Blog and I might Want to Suggest You To Read This Blog Bellsouth.Net Email Login Also. I doubtlessly accept that you will like it .

    ReplyDelete
  36. I'm always looking for free stuff on the web. There are other companies that offer free models.
    Please refer to the article mouse scroll jumps mac to know more about How to fix mouse wheel scrolling wrong way. Here you will read mouse scroll jumps mac. Thank you for your attention!

    ReplyDelete
  37. The things posted here on the blog is so much informative but the main thing which is responsible here is the assignment help service which is given by GoAssignmentHelp which is the best company so far we have seen and it is really as simple as we know till now with the best academic writing services which gives the online assignment help at the best prices without the fear of getting caught by the examiner.

    ReplyDelete
  38. 파칭코사이트인포I would like to express appreciation to this writer just for bailing me out of this particular challenge. After looking through the online world and seeing recommendations which were not helpful, I thought my entire life was over. Existing without the presence of answers to the issues you have solved all through this short article is a crucial case, as well as the kind that could have adversely affected my entire career if I hadn’t discovered your blog. Your good training and kindness in taking care of all the details was tremendous. I’m not sure what I would have done if I hadn’t discovered such a step like this. I can also at this point relish my future. Thank you very much for your reliable and effective help. I won’t be reluctant to suggest your web blog to anyone who needs and wants assistance about this topic.

    ReplyDelete
  39. 슬롯머신777사이트I would like to express appreciation to this writer just for bailing me out of this particular challenge. After looking through the online world and seeing recommendations which were not helpful, I thought my entire life was over. Existing without the presence of answers to the issues you have solved all through this short article is a crucial case, as well as the kind that could have adversely affected my entire career if I hadn’t discovered your blog. Your good training and kindness in taking care of all the details was tremendous. I’m not sure what I would have done if I hadn’t discovered such a step like this. I can also at this point relish my future. Thank you very much for your reliable and effective help. I won’t be reluctant to suggest your web blog to anyone who needs and wants assistance about this topic.

    ReplyDelete
  40. 블랙잭사이트I would like to express appreciation to this writer just for bailing me out of this particular challenge. After looking through the online world and seeing recommendations which were not helpful, I thought my entire life was over. Existing without the presence of answers to the issues you have solved all through this short article is a crucial case, as well as the kind that could have adversely affected my entire career if I hadn’t discovered your blog. Your good training and kindness in taking care of all the details was tremendous. I’m not sure what I would have done if I hadn’t discovered such a step like this. I can also at this point relish my future. Thank you very much for your reliable and effective help. I won’t be reluctant to suggest your web blog to anyone who needs and wants assistance about this topic.

    ReplyDelete
  41. 카지노사이트I would like to express appreciation to this writer just for bailing me out of this particular challenge. After looking through the online world and seeing recommendations which were not helpful, I thought my entire life was over. Existing without the presence of answers to the issues you have solved all through this short article is a crucial case, as well as the kind that could have adversely affected my entire career if I hadn’t discovered your blog. Your good training and kindness in taking care of all the details was tremendous. I’m not sure what I would have done if I hadn’t discovered such a step like this. I can also at this point relish my future. Thank you very much for your reliable and effective help. I won’t be reluctant to suggest your web blog to anyone who needs and wants assistance about this topic.

    ReplyDelete
  42. I would like to express appreciation to this writer just for bailing me out of this particular challenge. After looking through the online world and seeing recommendations which were not helpful, I thought my entire life was over. Existing without the presence of answers to the issues you have solved all through this short article is a crucial case, as well as the kind that could have adversely affected my entire career if I hadn’t discovered your blog. Your good training and kindness in taking care of all the details was tremendous. I’m not sure what I would have done if I hadn’t discovered such a step like this. I can also at this point relish my future. Thank you very much for your reliable and effective help. I won’t be reluctant to suggest your web blog to anyone who needs and wants assistance about this topic.토토사이트

    ReplyDelete
  43. There are many areas where Capsim application can be found, including the military. Each branch of the armed forces has their own training and classroom simulator that uses a CAPSIM-like program to help teach different tactics or strategies. Students have reported working on CAPSIM programs in nursing school as well as business schools. Read more on Capsim Homework Help

    ReplyDelete
  44. Assignmentand Homework Help inToronto
    Our Assignment Help in Toronto services are becoming very popular these days because in this growing age of education, students pursuing their degrees at various colleges and universities in Toronto find it very difficult to find time to complete their assignments. Toronto has a large number of students who spend most of their college and university years on repair projects and assignments. However, the weight of the assignments depends on the number of courses chosen by the students. Writing assignments is a daunting task, so students enlist the help of Assignment Help in Toronto Services. Thetutorshelp.com provides excellent assignment support to Toronto authors who specialize in this area and have earned honorary degrees and professional degrees from top Toronto universities.
    https://www.thetutorshelp.com/assignment-help-toronto.php

    Assignmentand Homework Help inToronto

    ReplyDelete
  45. webgirls In relation to battling yeast infections, patients often have their work eliminate for them. It is because yeast infections can certainly grow to be constant and continuing. With that in mind, on this page, we are going to existing a selection of some of the best proven yeast infection treatment and elimination recommendations about.

    ReplyDelete
  46. https://gamebegin.xyz You may exercise by yourself. A pitching equipment allows you to set the speed of the tennis ball. By loading a number of baseballs to the machine, it is possible to exercise hitting without having a pitcher. This digital machine is ideal for those who would like to practice baseball by itself. Pitching models could be acquired at the community athletic goods retailer.

    ReplyDelete
  47. https://gameboot.xyz The truth is them on periodicals and also on Television set, women and men who look like their forearms and legs will explode as their muscles are extremely massive! There is absolutely no will need that you should get your system for that level should you don't desire to, because the simple techniques in this article will enable you to build muscles in a healthy way.

    ReplyDelete
  48. revenue accounts Hello there! This post couldn’t be written any better! Reading this post reminds me of my previous room mate!

    ReplyDelete
  49. Thanks for telling me about most of the wisdom drives. I am grateful to have come across this blog today. I'm one of your new viewers. Thanks for the update. free catering craft  past questions pdf

    ReplyDelete
  50. I am truly pleased to glance at this weblog posts which includes plenty of useful information, thanks for providing these data. Thank you for sharing.- snapnet past questions & solutions

    ReplyDelete
  51. Hi Dear,
    I really prefer your blog..! This blog are very useful for me and other. So I see it every day.

    Watch free latest Hindi and English movies online at pikflix. Best source to watch and download dubbed Hindi & English movies in HD Quality.

    Visit Now - https://www.pikflix.in/en/movies'

    Thanks

    ReplyDelete
  52. arbys breakfast menu Hello there! This post couldn’t be written any better! Reading this post reminds me of my previous room mate!

    ReplyDelete
  53. <a href="https://thegrillshopboyertown.com/is-the-boulder-lodge-bar-and-grill-still-open/''>Is the Boulder Lodge Bar and Grill Still Open?</a> Hello there! This post couldn’t be written any betterIs the Boulder Lodge Bar and Grill Still Open?! Reading this post reminds me of my previous room mate!

    ReplyDelete