Everything You Need to Know About Bash For Loops in Linux
As a Linux user, you might often come across scenarios where you need to iterate through a set of items or a list of files. In such cases, bash for loops can come in handy. In this article, we’ll cover everything you need to know about bash for loops in Linux.
What is a bash for loop?
In a bash for loop, you essentially iterate through a set of items, perform some kind of action on each item, and then move on to the next item. The loop continues until you’ve iterated through the entire set of items.
The basic syntax of a bash for loop is as follows:
“`
for ITEM in SET_OF_ITEMS
do
# Action to be performed on each ITEM
done
“`
Here, `ITEM` is a variable that represents each item in the set, and `SET_OF_ITEMS` is the set of items that you want to iterate through. You can perform any kind of action on each item within the loop, such as reading a file or processing data.
Example of a simple for loop
Let’s look at a simple example of a bash for loop where we print the numbers 1 to 5:
“`
#!/bin/bash
for i in 1 2 3 4 5
do
echo $i
done
“`
In this example, `i` represents each number in the set from 1 to 5. The loop iterates through the set and prints each number using the `echo` command.
Using variables in a for loop
You can also use variables in a bash for loop. For example, let’s say you want to iterate through a set of file names stored in a variable:
“`
#!/bin/bash
FILES=”file1.txt file2.txt file3.txt”
for f in $FILES
do
echo Processing $f
# Perform some action on $f
done
“`
In this example, `FILES` is a variable that contains a set of file names separated by spaces. The loop iterates through the set and performs some action on each file.
Using wildcards in a for loop
You can also use wildcards to iterate through a set of files that match a specific pattern. For example, if you want to iterate through all files with the .txt extension in a directory:
“`
#!/bin/bash
for f in *.txt
do
echo Processing $f
# Perform some action on $f
done
“`
In this example, `*.txt` is a wildcard pattern that matches all files in the current directory with the .txt extension. The loop iterates through the set and performs some action on each file.
Using counter variables in a for loop
You can also use a counter variable to track the number of iterations in a for loop. For example, let’s say you want to iterate through a set of numbers and print out each number along with its position in the set:
“`
#!/bin/bash
NUMBERS=”1 2 3 4 5″
count=1
for n in $NUMBERS
do
echo $count: $n
((count++))
done
“`
In this example, `count` is a variable that tracks the number of iterations in the loop. The loop iterates through the set and prints out each number along with its position in the set.
Conclusion
Bash for loops are a powerful tool for iterating through sets of items in Linux. In this article, we covered the basic syntax of a bash for loop, how to use variables and wildcards in a for loop, and how to use counter variables to track the number of iterations. With this knowledge, you should be able to use bash for loops to perform a wide range of tasks in Linux.