Bash Script Using For Loop

LinuxLinuxBeginner
Practice Now

Introduction

Bash is a powerful scripting language that can be used to automate tasks in a Linux environment. One of the most commonly used constructs in Bash is the for loop, which allows you to iterate over a set of values and perform actions on them.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL linux(("`Linux`")) -.-> linux/BasicSystemCommandsGroup(["`Basic System Commands`"]) linux(("`Linux`")) -.-> linux/InputandOutputRedirectionGroup(["`Input and Output Redirection`"]) shell(("`Shell`")) -.-> shell/BasicSyntaxandStructureGroup(["`Basic Syntax and Structure`"]) shell(("`Shell`")) -.-> shell/VariableHandlingGroup(["`Variable Handling`"]) shell(("`Shell`")) -.-> shell/ControlFlowGroup(["`Control Flow`"]) shell(("`Shell`")) -.-> shell/AdvancedScriptingConceptsGroup(["`Advanced Scripting Concepts`"]) linux/BasicSystemCommandsGroup -.-> linux/echo("`Text Display`") linux/InputandOutputRedirectionGroup -.-> linux/redirect("`I/O Redirecting`") linux/BasicSystemCommandsGroup -.-> linux/logical("`Logic Operations`") linux/BasicSystemCommandsGroup -.-> linux/printf("`Text Formatting`") 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/AdvancedScriptingConceptsGroup -.-> shell/arith_expansion("`Arithmetic Expansion`") shell/AdvancedScriptingConceptsGroup -.-> shell/subshells("`Subshells and Command Groups`") shell/AdvancedScriptingConceptsGroup -.-> shell/adv_redirection("`Advanced Redirection`") subgraph Lab Skills linux/echo -.-> lab-387356{{"`Bash Script Using For Loop`"}} linux/redirect -.-> lab-387356{{"`Bash Script Using For Loop`"}} linux/logical -.-> lab-387356{{"`Bash Script Using For Loop`"}} linux/printf -.-> lab-387356{{"`Bash Script Using For Loop`"}} shell/shebang -.-> lab-387356{{"`Bash Script Using For Loop`"}} shell/comments -.-> lab-387356{{"`Bash Script Using For Loop`"}} shell/quoting -.-> lab-387356{{"`Bash Script Using For Loop`"}} shell/variables_decl -.-> lab-387356{{"`Bash Script Using For Loop`"}} shell/variables_usage -.-> lab-387356{{"`Bash Script Using For Loop`"}} shell/for_loops -.-> lab-387356{{"`Bash Script Using For Loop`"}} shell/arith_expansion -.-> lab-387356{{"`Bash Script Using For Loop`"}} shell/subshells -.-> lab-387356{{"`Bash Script Using For Loop`"}} shell/adv_redirection -.-> lab-387356{{"`Bash Script Using For Loop`"}} end

Bash Script Using For Loop

Create a Bash script named ~/project/for_example.sh that uses a 'for' loop to iterate ten times and print each value of the 'counter' variable on a single line.

Requirements

To successfully complete this challenge, the script should:

  • Use a 'for' loop that iterates ten times
  • Declare a 'counter' variable that starts at 10 and decrements by 1 with each iteration
  • Print each value of the 'counter' variable on a single line, separated by spaces

Example

To run the script, use the following command:

bash for_example.sh

Output:

10 9 8 7 6 5 4 3 2 1

The script will output the values from 10 to 1 on a single line.

Summary

In this challenge, you have learned how to use the for loop construct in Bash scripting to iterate over a set of values and perform actions on them. By following the requirements and creating your own Bash script, you have demonstrated your understanding of this concept and your ability to apply it in a practical setting.

Other Linux Tutorials you may like