Programs for printing different patterns in Bash

Given the number N which represents the number of rows and columns, print the different following patterns in Bash.

  • Pattern-1:
    Input: 6
    Output:
         #
        ##
       ###
      ####
     #####
    ######
    
    

    Use nested loop to print the given pattern. The first loop represents the row and the second loop represents the column.

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    # Program to print the
    # given pattern
      
    # Static input for N
    N=5
      
    # variable used for
    # while loop
    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
              # Print the pattern
              echo -ne "#"
            else
              # Print the spaces required
              echo -ne " "
            fi
            j=`expr $j + 1`
        done
        # For next line
        echo
                   
        i=`expr $i + 1`
    done

    chevron_right

    
    

    Output:



        #
       ##
      ###
     ####
    #####
    
  • Pattern-2:
    Input: 3
    Output:
        #
       ###
      #####
    

    Use nested loops to print the left part and right part of the pattern. The details are explained in the code:

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    # Program in Bash to 
    # print pyramid
      
    # Static input to the
    # number
    p=7;
      
    for((m=1; m<=p; m++))
    do
        # This loop print spaces
        # required
        for((a=i; a<=p; a++))
        do
          echo -ne " ";
        done
          
        # This loop print the left
        # side of the the pyramid
        for((n=1; n<=m; n++))
        do
          echo -ne "#";
        done
      
        # This loop print right 
        # side of the pryamid.
        for((i=1; i<m; i++))
        do
          echo -ne "#";
        done
      
        # New line
        echo;
    done

    chevron_right

    
    

    Output:

            #
           ###
          #####
         #######
        #########
       ###########
      #############
    


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :

1


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.