Different Types of Patterns in shell script

Welcome back!!!! Her in this version of our post we are going to see some tricky patterns in linux shell script or bash programming. I hope the code I have made for this pattern program is surly written in easy way for beginner as well as professional. This post included some of the most tricky patterns like star triangle, star pyramid ,inverted pyramid, alphabet triangle,alphabet pyramid,number triangle number pyramid and so on.

Any way let us enjoy with code…………………………

1.A SHELL SCRIPT TO DISPLAY TRIANGULAR PATTERN USING STAR(*)

# A SHELL SCRIPT TO DISPLAY STAR(*) TRIANGLE

#!/bin/sh
echo "enter size"
read n

for((i=1; i<=n; i++))
do 
  for((j=1; j<=i;j++))
  do
    echo -n "* "
  done
  echo 
done

OUT PUT

enter size of your pattern
7
your triangular pattern is:
* 
* * 
* * * 
* * * * 
* * * * * 
* * * * * * 
* * * * * * * 

2. A SHELL SCRIPT TO DISPLAY INVERTED TRIANGULAR PATTERN USING STAR(*)

# A SHELL SCRIPT TO DISPLAY STAR(*) TRIANGLE

#!/bin/sh
echo "enter size"
read n

for((i=n; i>=1; i--)
do 
  for((j=1; j<=i;j++))
  do
    echo -n "* "
  done
  echo 
done

OUT PUT

enter size of your pattern
7
your triangular pattern is:
* * * * * * * 
* * * * * * 
* * * * * 
* * * * 
* * * 
* * 
* 

3.A SHELL SCRIPT CODE TO DISPLAY TRIANGULAR NUMBER PATTERN

 #A SHELL SCRIPT TO DISPLAY STAR(*) TRIANGLE

#!/bin/sh
echo "enter size of your pattern"
read n
echo "your triangular pattern is:"
for((i=1; i<=n; i++))
do 
  for((j=1; j<=i;j++))
  do
    echo -n "$j "
  done
  echo 
done







OUT PUT

enter size of your pattern
7
your triangular pattern is:
1 
1 2 
1 2 3 
1 2 3 4 
1 2 3 4 5 
1 2 3 4 5 6 
1 2 3 4 5 6 7 

4.A SHELL SCRIPT CODE TO DISPLAY INVERTED NUMBER TRIANGLE

 #A SHELL SCRIPT TO DISPLAY STAR(*) TRIANGLE

#!/bin/sh
echo "enter size of your pattern"
read n
echo "your triangular pattern is:"
for((i=n; i>=1; i--))
do 
  for((j=1; j<=i;j++))
  do
    echo -n "$j "
  done
  echo 
done







OUT PUT

enter size of your pattern
7
your triangular pattern is:
1 2 3 4 5 6 7 
1 2 3 4 5 6 
1 2 3 4 5 
1 2 3 4 
1 2 3 
1 2 
1 

5.A SHELL SCRIPT CODE TO DISPLAY PASCAL’S TRIANGLE

#A SHELL SCRIPT CODE TO PRINT PASCAL'S TRIANGLE

#!/bin/sh
coff=1
echo "enter size of your triangle"
read n
for((i=0; i<=n; i++))
do
  for((space=1; space<=n-i; space++))
  do
    echo -n " "
  done
  for((j=0; j<=i; j++))
  do
    if [[ $j == 0 || $i == 0 ]]
    then
        coff=1
   else
      coff=$((coff*(i-j+1)/j))
  fi
  echo -n $coff " "
done
echo " "
done
OUT PUT

enter size of your triangle
4
    1   
   1  1   
  1  2  1   
 1  3  3  1   
1  4  6  4  1   

6.A shell script code to display Floyd’s triangle


# shell script to display Floyd's triangle

#!/bin/sh
k=1
echo "enter the size of your pattern"
read row
echo "your Floyd's triangle is:"

for((i=1; i<row; ++i))
do
  for((j=1; j<=i; ++j))
  do
    echo -n "$k "
    ((++k))
  done
  echo
done
OUT PUT

enter the size of your pattern
5
your Floyd's triangle is:
1 
2 3 
4 5 6 
7 8 9 10 

7.A shell script code to display pyramid of pattern using star(*)

 #A SHELL SCRIPT TO DISPLAY STAR(*) TRIANGLE

#!/bin/sh
echo "enter size of your pattern"
read n
echo "your pyramidal pattern is:"
for((i=1; i<=n; i++))
do 
  for((space=1; space<=n-i; space++))
  do
     echo -n " "
  done
  for((j=1; j<=i; j++))
  do
    echo -n "* "
  done
  echo 
done







OUT PUT

enter size of your pattern
7
your pyramidal pattern is:
      * 
     * * 
    * * * 
   * * * * 
  * * * * * 
 * * * * * * 
* * * * * * * 

8.A shell script code to display inverted pyramidal pattern using star(*)

 #A SHELL SCRIPT TO DISPLAY STAR(*) TRIANGLE

#!/bin/sh
echo "enter size of your pattern"
read n
echo "your pyramidal pattern is:"
for((i=n; i>=1; i--))
do 
  for((space=1; space<=n-i; space++))
  do
     echo -n " "
  done
  for((j=1; j<=i; j++))
  do
    echo -n "* "
  done
  echo 
done







OUT PUT

enter size of your pattern
7
your pyramidal pattern is:
* * * * * * * 
 * * * * * * 
  * * * * * 
   * * * * 
    * * * 
     * * 
      * 

9.A shell script code to display alphabet triangular pattern


# shell scrip to display pyramid of alphabet

#!/bin/sh
alpha=( {A..Z} )
echo "enter the size of your pattern"
read row
echo "alphabet pattern of size $row is:"

for((i=1; i<=row; i++))
do

   for((j=0; j<i; j++))
   do 
      echo -n "${alpha[j]} "

   done
   echo
done
OUT PUT

enter the size of your pattern
7
pattern of size 7 is:
A 
A B 
A B C 
A B C D 
A B C D E 
A B C D E F 
A B C D E F G 

10.A shell script code to display inverted alphabet triangular pattern

# shell scrip to display pyramid of alphabet

#!/bin/sh
alpha=( {A..Z} )
echo "enter the size of your pattern"
read row
echo "alphabet pattern of size $row is:"

for((i=row; i>=1; i--)
do

   for((j=0; j<i; j++))
   do 
      echo -n "${alpha[j]} "

   done
   echo
done
OUT PUT

enter the size of your pattern
7
pattern of size 7 is:
A B C D E F G 
A B C D E F 
A B C D E 
A B C D 
A B C 
A B 
A 

11.A shell script code to display alphabet pyramid


# shell scrip to display pyramid of alphabet

#!/bin/sh
alpha=( {A..Z} )
echo "enter the size of your pattern"
read row
echo "pattern of size $row is:"
for((i=1; i<=row; i++))
do
   for((space=1; space<=row-i; space++))
   do
      echo -n " "
   done

   for((j=0; j<i; j++))
   do 
      echo -n "${alpha[j]} "

   done
   echo
done
OUT PUT

enter the size of your pattern
7
pattern of size 7 is:
      A 
     A B 
    A B C 
   A B C D 
  A B C D E 
 A B C D E F 
A B C D E F G 

12.A shell script code to display inverted alphabet of pyramid


# shell scrip to display pyramid of alphabet

#!/bin/sh
alpha=( {A..Z} )
echo "enter the size of your pattern"
read row
echo "pattern of size $row is:"
for((i=row; i>=1; i--))
do
   for((space=1; space<=row-i; space++))
   do
      echo -n " "
   done

   for((j=0; j<i; j++))
   do 
      echo -n "${alpha[j]} "

   done
   echo
done
OUT PUT

enter the size of your pattern
6
pattern of size 6 is:
A B C D E F 
 A B C D E 
  A B C D 
   A B C 
    A B 
     A 

13.A shell script to display binary triangular number pattern

finally I would like to show you one more unique pattern which is of binary number form. we can create any type of binary number pattern but now i will write one code to display mixed binary digit pattern which is little tricky the pattern looks like as the one given below. ya exactly it’s so beautiful let see the code also

1 
0 1 
1 0 1 
0 1 0 1 
1 0 1 0 1 
0 1 0 1 0 1 
1 0 1 0 1 0 1 

See the code below it seems difficult but chill it is just easy......
# A SHELL SCRIPT CODE TO DISPLAY PATTERN
# OF BINARY DIGIT 

#!/bin/sh
coff=1
echo "enter size of your pattern"
read n
echo "The binary number pattern of size $n is:"
for((i=1; i<=n; i++))
 do
   for((j=1; j<=i; j++))
   do
      if (( (i+j)%2==0 ))
      then
         echo -n "1 "
      else
         echo -n "0 "
    fi
   done
   echo
done

OUT PUT
enter size of your pattern
7
The binary number pattern of size 7 is:

1 
0 1 
1 0 1 
0 1 0 1 
1 0 1 0 1 
0 1 0 1 0 1 
1 0 1 0 1 0 1 

14.A shell script to display inverted pattern of binary number

Here I am going to show you the inverted binary number pattern in a triangular form which is given above in a normal form

# A SHELL SCRIPT CODE TO DISPLAY PATTERN
# OF BINARY DIGIT 

#!/bin/sh
coff=1
echo "enter size of your pattern"
read n
echo "The binary number pattern of size $n is:"
for((i=n; i>=1; i--)
 do
   for((j=1; j<=i; j++))
   do
      if (( (i+j)%2==0 ))
      then
         echo -n "1 "
      else
         echo -n "0 "
    fi
   done
   echo
done
OUT PUT

enter size of your pattern
7
The inverted binary pattern of size 7 is:
1 0 1 0 1 0 1 
0 1 0 1 0 1 
1 0 1 0 1 
0 1 0 1 
1 0 1 
0 1 
1 

15.A shell script to display pyramidal binary number pattern

Here also I am going to show you how to display the binary number format given above in a pyramidal format

let’s go here is the code.

# A SHELL SCRIPT CODE TO DISPLAY PATTERN
# OF BINARY DIGIT 

#!/bin/sh
coff=1
echo "enter size of your pattern"
read n
echo "The pyramidal binary pattern of size $n is:"
for((i=1; i<=n; i++))
do
   for((space=1; space<=n-i; space++))
   do
      echo -n " "
   done

   for((j=1; j<=i; j++))
   do
      if (( (i+j)%2==0 ))
      then
          echo -n "1 "
      else
         echo -n "0 "
     fi
    done
   echo
done

enter size of your pattern
7
The inverted binary pattern of size 7 is:
      1 
     0 1 
    1 0 1 
   0 1 0 1 
  1 0 1 0 1 
 0 1 0 1 0 1 
1 0 1 0 1 0 1 

16.A shell script to display an inverted pyramidal binary number pattern.

Finally I would like to show you the binary number inverted pyramidal pattern which is the end of this post.

# A SHELL SCRIPT CODE TO DISPLAY PATTERN
# OF BINARY DIGIT 

#!/bin/sh
coff=1
echo "enter size of your pattern"
read n
echo "The pyramidal binary pattern of size $n is:"
for((i=n; i>=1; i--))
do
   for((space=1; space<=n-i; space++))
   do
      echo -n " "
   done

   for((j=1; j<=i; j++))
   do
      if (( (i+j)%2==0 ))
      then
          echo -n "1 "
      else
         echo -n "0 "
     fi
    done
   echo
done

OUT PUT

enter size of your pattern
7
The pyramidal binary pattern of size 7 is:
1 0 1 0 1 0 1 
 0 1 0 1 0 1 
  1 0 1 0 1 
   0 1 0 1 
    1 0 1 
     0 1 
      1 

Thanks for your comment. please don’t forget to share the site to your friends.

Published by temamhashim

This is Temam Hashim certified Software Engineer from top leading University with highest score with full capablity of various top leading programming language as a software Engineer and ability to deal with logic and mathimatical problem.

Leave a comment

Design a site like this with WordPress.com
Get started