for loop example in linux shell scripting

Mastering Linux Shell: For Loop Examples

By Christopher Hernandez
AD

In the realm of Linux, shell scripting is a powerful tool that enables users to automate tasks and streamline workflows. One of the fundamental constructs in shell scripting is the 'for' loop, which allows for repetitive tasks to be executed efficiently. Let's delve into the world of 'for' loops in Linux shell scripting, exploring their syntax, usage, and practical examples.

The 'for' loop in Linux shell scripting is a control flow structure that repeats a block of code for a specified number of times or until a certain condition is met. It's particularly useful when you need to perform the same action on multiple items, such as processing a list of files or iterating through an array.

Understanding the Syntax of 'for' Loop

The basic syntax of a 'for' loop in Linux shell scripting is as follows:

AD
Introduction to Shell Scripting - Topic
Introduction to Shell Scripting - Topic
40 Simple Yet Effective Linux Shell and Bash Script Examples
40 Simple Yet Effective Linux Shell and Bash Script Examples
How to Run Shell Scripts in Linux [with Detailed Explanation for Beginners]
How to Run Shell Scripts in Linux [with Detailed Explanation for Beginners]

for variable in list; do command; done

Here's a breakdown of the syntax:

Bash if elif else Statement with Examples - Linux Shell Scripting Guide
Bash if elif else Statement with Examples - Linux Shell Scripting Guide
linux and windows reverse shell scripts with an image of a bird in the background
linux and windows reverse shell scripts with an image of a bird in the background
Linux Shell Scripting Command Cheat Sheet
Linux Shell Scripting Command Cheat Sheet
  • variable: A user-defined variable that will take on the value of each item in the list during each iteration.
  • in list: The 'in' keyword is followed by a list of items, which can be a string of space-separated values, a range of numbers, or even the output of a command.
  • do command; done: The 'do' keyword marks the beginning of the block of code to be executed for each item in the list. The 'done' keyword marks the end of this block.

Iterating through a List of Items

One of the most common uses of the 'for' loop is to iterate through a list of items. Here's an example that prints out a list of numbers from 1 to 5:

for i in 1 2 3 4 5; do echo $i; done

AD

In this example, the variable 'i' takes on the value of each number in the list during each iteration, and the 'echo' command prints out the current value of 'i'.

For Loop vs While Loop in Python โ€” Explained Simply ๐Ÿ
For Loop vs While Loop in Python โ€” Explained Simply ๐Ÿ
Kernel vs Shell โ€“ Core Concepts of Linux
Kernel vs Shell โ€“ Core Concepts of Linux
Unlock the Power of Linux with "The Linux Command Line"
Unlock the Power of Linux with "The Linux Command Line"
Master Python Loops: The Ultimate Beginnerโ€™s Cheat Sheet ๐Ÿ”„๐Ÿ
Master Python Loops: The Ultimate Beginnerโ€™s Cheat Sheet ๐Ÿ”„๐Ÿ
Linux Shell Guide
Linux Shell Guide
an image of a computer screen with text and symbols in neon colors on black background
an image of a computer screen with text and symbols in neon colors on black background

Iterating through a Range of Numbers

You can also use the 'for' loop to iterate through a range of numbers. Here's an example that prints out numbers from 1 to 5 using a range:

for i in {1..5}; do echo $i; done

AD

In this example, the curly braces '{' and '}' are used to denote a range, and the two dots '..' indicate that we want all numbers between 1 and 5 (inclusive).

a computer screen with some type of programming program on it's side, and another screenshot of the code
a computer screen with some type of programming program on it's side, and another screenshot of the code
Codin For Beginners Python - Learn the Basics - Loops
Codin For Beginners Python - Learn the Basics - Loops
Linux Shell script to display Files and Folders in Specific Directory | ls command in linux #shorts
Linux Shell script to display Files and Folders in Specific Directory | ls command in linux #shorts
Learn Linux from Beginner to Advanced (Part-8) | Shell & Productivity Commands
Learn Linux from Beginner to Advanced (Part-8) | Shell & Productivity Commands
shells
shells
How to Perform Syntax Checking Debugging Mode in Shell Scripts
How to Perform Syntax Checking Debugging Mode in Shell Scripts
A Shell Script to Monitor Network, Disk, Uptime, Load & RAM
A Shell Script to Monitor Network, Disk, Uptime, Load & RAM
the diagram shows how linux works and what it is doing to work with them on
the diagram shows how linux works and what it is doing to work with them on
a flow diagram with the words white - loop and an exit from the loop on it
a flow diagram with the words white - loop and an exit from the loop on it
a screenshot of a computer screen with the text, chatsheet for beginners linux commands
a screenshot of a computer screen with the text, chatsheet for beginners linux commands
7 Reasons Why You Should Install The Fish Shell
7 Reasons Why You Should Install The Fish Shell
Learn Linux from Beginner to Advanced (Part-10) | Automation & Scheduling Commands
Learn Linux from Beginner to Advanced (Part-10) | Automation & Scheduling Commands
How to Create & Run Shell Scripts on Windows [Step-By-Step]
How to Create & Run Shell Scripts on Windows [Step-By-Step]
the info sheet for linux's web application, which is designed to look like a computer
the info sheet for linux's web application, which is designed to look like a computer

Using 'for' Loop with Commands

The 'for' loop can also iterate through the output of a command. This is particularly useful when you want to perform an action on a dynamic list of items. Here's an example that prints out the names of all files in the current directory:

for file in $(ls); do echo $file; done

AD

In this example, the 'ls' command is used to list all files in the current directory, and the output is passed to the 'for' loop. The variable 'file' takes on the value of each file name during each iteration.

Iterating through an Array

In Linux shell scripting, you can also use arrays to store a list of items. Here's an example that prints out a list of colors stored in an array:

colors=("red" "green" "blue")

for color in "${colors[@]}"; do echo $color; done

In this example, the array 'colors' is declared with three elements: 'red', 'green', and 'blue'. The 'for' loop iterates through each element in the array, and the 'echo' command prints out the current value of 'color'.

Using 'for' Loop with Conditional Statements

You can also use conditional statements, such as 'if' and 'elif', inside a 'for' loop to control the flow of the loop based on certain conditions. Here's an example that prints out only even numbers from 1 to 10:

for i in {1..10}; do if [ $((i % 2)) -eq 0 ]; then echo $i; fi; done

In this example, the modulo operator '%' is used to check if the current value of 'i' is even. If it is, the 'echo' command prints out the number.

In conclusion, the 'for' loop is a powerful tool in Linux shell scripting that enables you to automate repetitive tasks and iterate through lists of items. Whether you're processing a list of files, iterating through a range of numbers, or using conditional statements, the 'for' loop provides a flexible and efficient way to streamline your workflows. So, go ahead and harness the power of 'for' loops to make your shell scripting more productive and enjoyable!

AD