Programs for printing different patterns in Bash
Last Updated :
10 Jun, 2022
Given the number N which represents the number of rows and columns, print the different following patterns in Bash.
Input: 6
Output:
#
##
###
####
#####
######
- Use nested loop to print the given pattern. The first loop represents the row and the second loop represents the column.
BASH
N=5
i=0
j=0
while [ $i - le ` expr $N - 1` ]
do
j=0
while [ $j - le ` expr $N - 1` ]
do
if [ ` expr $N - 1` - le ` expr $i + $j` ]
then
echo - ne "#"
else
echo - ne " "
fi
j=` expr $j + 1`
done
echo
i=` expr $i + 1`
done
|
Output:
#
##
###
####
#####
Input: 3
Output:
#
###
#####
- Use nested loops to print the left part and right part of the pattern. The details are explained in the code:
BASH
p=7;
for ((m=1; m<=p; m++))
do
for ((a=m; a<=p; a++))
do
echo - ne " " ;
done
for ((n=1; n<=m; n++))
do
echo - ne "#" ;
done
for ((i=1; i<m; i++))
do
echo - ne "#" ;
done
echo ;
done
|
Output:
#
###
#####
#######
#########
###########
#############
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...