Print Equilateral Triangle Pattern

LinuxLinuxIntermediate
Practice Now

Introduction

In this challenge, we will learn how to use loops in a shell script to generate a specific pattern. We will read a value from the user and create a pattern based on that value. The pattern will be a series of numbers arranged in a specific format.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL shell(("`Shell`")) -.-> shell/ControlFlowGroup(["`Control Flow`"]) linux(("`Linux`")) -.-> linux/BasicSystemCommandsGroup(["`Basic System Commands`"]) linux(("`Linux`")) -.-> linux/InputandOutputRedirectionGroup(["`Input and Output Redirection`"]) linux(("`Linux`")) -.-> linux/TextProcessingGroup(["`Text Processing`"]) shell(("`Shell`")) -.-> shell/BasicSyntaxandStructureGroup(["`Basic Syntax and Structure`"]) shell(("`Shell`")) -.-> shell/VariableHandlingGroup(["`Variable Handling`"]) shell(("`Shell`")) -.-> shell/AdvancedScriptingConceptsGroup(["`Advanced Scripting Concepts`"]) shell(("`Shell`")) -.-> shell/SystemInteractionandConfigurationGroup(["`System Interaction and Configuration`"]) shell/ControlFlowGroup -.-> shell/if_else("`If-Else Statements`") linux/BasicSystemCommandsGroup -.-> linux/echo("`Text Display`") linux/InputandOutputRedirectionGroup -.-> linux/redirect("`I/O Redirecting`") linux/BasicSystemCommandsGroup -.-> linux/read("`Input Reading`") linux/TextProcessingGroup -.-> linux/col("`Line Feed Filtering`") shell/BasicSyntaxandStructureGroup -.-> shell/shebang("`Shebang`") shell/BasicSyntaxandStructureGroup -.-> shell/comments("`Comments`") shell/BasicSyntaxandStructureGroup -.-> shell/quoting("`Quoting Mechanisms`") shell/VariableHandlingGroup -.-> shell/variables_decl("`Variable Declaration`") shell/VariableHandlingGroup -.-> shell/variables_usage("`Variable Usage`") shell/ControlFlowGroup -.-> shell/for_loops("`For Loops`") shell/ControlFlowGroup -.-> shell/cond_expr("`Conditional Expressions`") shell/AdvancedScriptingConceptsGroup -.-> shell/arith_expansion("`Arithmetic Expansion`") shell/AdvancedScriptingConceptsGroup -.-> shell/read_input("`Reading Input`") shell/AdvancedScriptingConceptsGroup -.-> shell/cmd_substitution("`Command Substitution`") shell/AdvancedScriptingConceptsGroup -.-> shell/subshells("`Subshells and Command Groups`") shell/SystemInteractionandConfigurationGroup -.-> shell/globbing_expansion("`Globbing and Pathname Expansion`") subgraph Lab Skills shell/if_else -.-> lab-18303{{"`Print Equilateral Triangle Pattern`"}} linux/echo -.-> lab-18303{{"`Print Equilateral Triangle Pattern`"}} linux/redirect -.-> lab-18303{{"`Print Equilateral Triangle Pattern`"}} linux/read -.-> lab-18303{{"`Print Equilateral Triangle Pattern`"}} linux/col -.-> lab-18303{{"`Print Equilateral Triangle Pattern`"}} shell/shebang -.-> lab-18303{{"`Print Equilateral Triangle Pattern`"}} shell/comments -.-> lab-18303{{"`Print Equilateral Triangle Pattern`"}} shell/quoting -.-> lab-18303{{"`Print Equilateral Triangle Pattern`"}} shell/variables_decl -.-> lab-18303{{"`Print Equilateral Triangle Pattern`"}} shell/variables_usage -.-> lab-18303{{"`Print Equilateral Triangle Pattern`"}} shell/for_loops -.-> lab-18303{{"`Print Equilateral Triangle Pattern`"}} shell/cond_expr -.-> lab-18303{{"`Print Equilateral Triangle Pattern`"}} shell/arith_expansion -.-> lab-18303{{"`Print Equilateral Triangle Pattern`"}} shell/read_input -.-> lab-18303{{"`Print Equilateral Triangle Pattern`"}} shell/cmd_substitution -.-> lab-18303{{"`Print Equilateral Triangle Pattern`"}} shell/subshells -.-> lab-18303{{"`Print Equilateral Triangle Pattern`"}} shell/globbing_expansion -.-> lab-18303{{"`Print Equilateral Triangle Pattern`"}} end

Pattern Printing Challenge

In this challenge, you are required to generate a specific pattern based on user input using loops in a bash script.

Tasks

You need to complete the following task:

  1. Read a value from the user and create a pattern as mentioned below.

Requirements

You must meet the following requirements:

  • Script name: pattern2.sh
  • Work directory: ~/project
  • Familiarity with running loops in shell scripts
  • Understanding how to execute a bash script
  • Knowledge of how to change execute permission of a file

Example

Input

5

Output

1
2 3
4 5 6
7 8 9 10
11 12 13 14 15

This challenge aims to help you understand the working of loops in a script.

Summary

After completing this challenge, we will have a better understanding of how to use loops in a shell script. We will also learn how to read input from the user and generate a pattern based on that input. This will help us in writing more complex shell scripts that involve loops and user input.

Other Linux Tutorials you may like