In this example, you will learn to print half pyramids, inverted pyramids, full pyramids, inverted full pyramids and Floyd's triangle in Bash scripting.
Half Pyramid of *
*
* *
* * *
* * * *
* * * * *
Script
#!/bin/bash
printf "Enter num of Rows:"
read rows
for ((i=1;i<=rows;i++))
do
for ((j=1;j<=i;j++))
do
printf "* "
done
printf "\n"
done
![bash_half_pyramid bash_half_pyramid](https://blogger.googleusercontent.com/img/a/AVvXsEgbsmbf3ZRR9JxKoOeF2xxIvBeU8WY5Nj64wqKXUd466xt_9__s1adBjrjMKrUwAIzb-sXWQJW7FjGofLrwRot8k6tj-12sUyuzfmwxqgMsnhDox6XKdtwKUtNMmdcnEo9RSOHtybCucA96-fRYYqHQc0ZAugx1QweYuLObShQOM225AcNserkMFK22=w640-h538) |
bash_half_pyramid |
Half Pyramid of 0
Enter num of Rows:5
0
0 0
0 0 0
0 0 0 0
0 0 0 0 0
Script
#!/bin/bash
printf "Enter num of Rows:"
read rows
for ((i=1;i<=rows;i++))
do
for ((j=1;j<=i;j++))
do
printf "0 "
done
printf "\n"
done
![bash_half_pyramid-0 bash_half_pyramid-0](https://blogger.googleusercontent.com/img/a/AVvXsEhsftbsMcntILk1na5BOU230hwIa_SPaRzUqGohtzcKaVny8leVDMYmD68UtWXCqMBqhrixbT4ZH2-SOErgz3vYKO7xX4gibQE_TViQra9MtZH0xIuggTO0QWxVqW0ln3I30qlNxY9TSymjgYd1Jvl2Ux2izhaYhEL-fpqpXtpjHECnY02MNpeukbz3=w640-h546) |
bash_half_pyramid-0 |
Half Pyramid of numbers
Enter num of Rows:5
1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
Script
#!/bin/bash
printf "Enter num of Rows:"
read rows
num=1
for ((i=1;i<=rows;i++))
do
for ((j=1;j<=i;j++))
do
printf "$num "
num=$((num + 1))
done
num=1
printf "\n"
done
![Half Pyramid of numbers Bash Half Pyramid of numbers Bash](https://blogger.googleusercontent.com/img/a/AVvXsEjohLTUpPZ0mTI4u07S_P97a4U5AYXtvGKWY9osNpOMhHALQeH3b39nxnzxRx-8VuQXE9OkkwxtQlbV3RACAXMfC7FMgSHhHqMSvRfDdP5sxlVYZSKw5rbPKmdhDeIjUhztxVYBZQm_ylRfu4v3Lofp99_svBGboU-gZm0VhcRxY4jvt-mWPQBxoDRF=w640-h422) |
Half Pyramid of numbers Bash |
Inverted Half Pyramid of *
Enter num of Rows:5
* * * * *
* * * *
* * *
* *
*
Script
#!/bin/bash
printf "Enter num of Rows:"
read rows
for ((i=rows;i>=1;i--))
do
for ((j=1;j<=i;j++))
do
printf "* "
done
printf "\n"
done
![Inverted Half Pyramid of star Inverted Half Pyramid of star](https://blogger.googleusercontent.com/img/a/AVvXsEjukBJ1sHopddX6IKt6uwxsyAHq1mudwGbSB3hIRaBdZQ4f1Eu29PUOkE9gwAgiaKrVug_4C136Vi7DiSJl5-4Srpi9vWW58MbOm-zPwvJsGOKjo9klET5yvpiEJRYQ3yv8xc-DDzAgXhDKOa583oeSyKB2Y_AobwU_VAPSuGoU7Vn3kkckER-bMtY8=w640-h504) |
Inverted Half Pyramid of star |
Full Pyramid of *
Enter num of Rows:5
*
* * *
* * * * *
* * * * * * *
* * * * * * * * *
Script
#!/bin/bash
printf "Enter num of Rows:"
read rows
for((i=1; i<=rows; i++))
do
for((j=1; j<=rows - i; j++))
do
printf " "
done
for((j=1; j<=2*i - 1; j++))
do
printf "* "
done
echo
done
![Full Pyramid of star Full Pyramid of star](https://blogger.googleusercontent.com/img/a/AVvXsEjUXW3-4by4CMMS9XC_HoHKu-1ez9UQJijFscyhEB4v8EOPZqWqhGVP5NSoPZQCZcmjhBvZaCPQlw2ycVRHjU2KX2F_M17hATzEqi7NFYc40fxDs2shAK8rlLtHJpEAh5IKb-Ps5GavNQBlJpuirZDxyLq6b60T4D8kOKf0CKnFF8EJSjBqV6dKDY4o=w640-h534) |
Full Pyramid of star |
Inverted Full Pyramid of *
Enter num of Rows:5
* * * * * * * * *
* * * * * * *
* * * * *
* * *
*
Script
#!/bin/bash
printf "Enter num of Rows:"
read rows
for((i=rows; i>=1; i--))
do
for((j=1; j<=rows - i; j++))
do
printf " "
done
for((j=1; j<=2*i - 1; j++))
do
printf "* "
done
echo
done
![Inverted Full Pyramid of star Inverted Full Pyramid of star](https://blogger.googleusercontent.com/img/a/AVvXsEgDoNSSs6XIs4yrLroy6rYoFK_nk1jXECHJqnesru0bmk9OkoTQnE_4tmymDsVeev0Bx2T2cBhnuMzOWzoYLx4JFWMuCiHQY9ZdDVP977dFHT2vuYznVm1Jces6g8-4lTF5X53wjP7Jctchdqa7OEj1DfxEDAd3rvdrhRkQgu8cXZWHdD7bFGnNIY7I=w640-h500) |
Inverted Full Pyramid of star |
Floyd's Triangle
Enter num of Rows:10
1
2 3
4 5 6
7 8 9 10
11 12 13 14 15
16 17 18 19 20 21
22 23 24 25 26 27 28
29 30 31 32 33 34 35 36
37 38 39 40 41 42 43 44 45
46 47 48 49 50 51 52 53 54 55
Script
#!/bin/bash
printf "Enter num of Rows:"
read rows
num=1
for((i=1; i<=rows; i++))
do
for((j=1; j<=i; j++))
do
echo -n "$num "
num=$((num + 1))
done
echo
done
![Floyds Triangle Floyds Triangle](https://blogger.googleusercontent.com/img/a/AVvXsEjVAbD35xInFJyNnEE74zNVloIgyiSZqXwVgewjWcZXDz7XOm7F4abBvyluHUtCEonYCoepdgR1Ry64kjzRZfvFwFouOyQq-IwyXvkUM8oxufKrq7aSUYu_bSjKpzyn6sRJUnrDdVKp-tYtfDlvT5emZ0e0qrtNiyflA7c66u7qrSYqvqOG643Fe14W=w640-h600) |
Floyds Triangle |
No comments