List of content you will read in this article:
A for loop is a fundamental programming construct allowing you to execute a code block multiple times. In Bash, for loops are used to iterate over a range of values or a list of items. In this article, we will explore the basics of Bash for loops and provide examples of how they can be used in practice.
Syntax of Bash For Loop
The basic syntax of a Bash for loop is as follows:
```bash
for variable in list
do
# commands to be executed
done
```
Here, `variable` is a user-defined variable that will take on each value in the `list` during each iteration of the loop. The `list` can be a range of values specified using brace expansion or a list of items separated by spaces. The commands to be executed during each iteration are enclosed within the `do` and `done` keywords.
Example 1: Iterating over a range of values
In this example, we will use a for loop to iterate over a range of values specified using brace expansion. We will print out the value of the variable during each iteration.
```bash
for i in {1..5}
do
echo $i
done
```
Output:
```
1
2
3
4
5
```
Here, we have used brace expansion to specify a range of values from 1 to 5. During each iteration of the loop, the value of `i` is printed out using the `echo` command.
Example 2: Iterating over a list of items
In this example, we will use a for loop to iterate over a list of items. We will print out each item during each iteration.
```bash
for item in apple banana orange
do
echo $item
done
```
Output:
```
apple
banana
orange
```
Here, we have specified a list of items separated by spaces. During each iteration of the loop, the value of `item` is printed out using the `echo` command.
Example 3: Reading from a file
In this example, we will use a for loop to read lines from a file. We will print out each line during each iteration.
```bash
for line in $(cat file.txt)
do
echo $line
done
```
Output:
```
This is line 1.
This is line 2.
This is line 3.
```
Here, we have used command substitution to read the contents of `file.txt` and store them in the `line` variable. During each iteration of the loop, the value of `line` is printed out using the `echo` command.
🚀 Power up your website with our high-performance VPS hosting! 💻💡 Unleash the speed and scalability of dedicated resources, elevating your online presence to new heights. 🌐📈
Example 4: Nested for loops
In this example, we will use nested for loops to iterate over two lists of items. We will print out each item pair during each iteration.
```bash
for item1 in apple banana orange
do
for item2 in red green blue
do
echo $item1 $item2
done
done
```
Output:
```
apple red
apple green
apple blue
banana red
banana green
banana blue
orange red
orange green
orange blue
```
Here, we have used two nested for loops to iterate over two lists of items. During each iteration of the outer loop, the inner loop iterates over all items in its list. The value of `item1` and `item2` is printed out during each iteration using the `echo` command.
Example 5: Using C-style for loops
In addition to the basic for loop syntax, Bash also supports C-style for loops. These loops are similar to those found in other programming languages and allow you to specify an initialization statement, a condition, and an update statement.
```bash
for ((i=0; i<5; i++))
do
echo $i
done
```
Output:
```
0
1
2
3
4
```
Here, we have used a C-style for loop to iterate over a range of values from 0 to 4. During each iteration of the loop, the value of `i` is printed out using the `echo` command.
Conclusion
In this article, we have explored the basics of Bash for loops and provided examples of how they can be used in practice. Whether you need to iterate over a range of values or a list of items, for loops are a powerful tool that can help you accomplish your programming tasks efficiently. By mastering this fundamental programming construct, you will be well on your way to becoming a proficient Bash programmer.
FAQs
1. What is a Bash for loop?
A Bash for loop is a programming construct that allows you to execute a block of code multiple times. It is used to iterate over a range of values or a list of items.
2. What is the syntax of a Bash for loop?
The basic syntax of a Bash for loop is as follows:
```bash
for variable in list
do
# commands to be executed
done
```
Here, `variable` is a user-defined variable that will take on each value in the `list` during each iteration of the loop. The commands to be executed during each iteration are enclosed within the `do` and `done` keywords.
3. How can I use a Bash for loop to iterate over a range of values?
You can use brace expansion to specify a range of values in a Bash for loop. For example:
```bash
for i in {1..5}
do
echo $i
done
```
This will print out the values 1 through 5 during each iteration of the loop.
4. How can I use a Bash for loop to iterate over a list of items?
You can specify a list of items separated by spaces in a Bash for loop. For example:
```bash
for item in apple banana orange
do
echo $item
done
```
This will print out the items "apple", "banana", and "orange" during each iteration of the loop.
5. How can I use a Bash for loop to read lines from a file?
You can use a Bash for loop to read lines from a file by using the `cat` command to read the file's contents and then specify the variable to hold each line. For example:
```bash
for line in $(cat file.txt)
do
echo $line
done
```
This will print out each line in the file "file.txt" during each iteration of the loop.
People also read:
I'm fascinated by the IT world and how the 1's and 0's work. While I venture into the world of Technology, I try to share what I know in the simplest way with you. Not a fan of coffee, a travel addict, and a self-accredited 'master chef'.