Bash For Loop by Examples
A Beginner's Guide

Introduction

In this article, we will explore the Bash for loop infor detail, discussing its syntax usage, providing and several code examples to illustrate its capabilities.

Syntax

The syntax of a Bash for loop is as follows:

for variable in list
do
  # Code block to be executed
done

Let's break down the various components of this syntax:

Using a For Loop with an Array

One common use case for a Bash for loop is to over an array of values. Let's look at an example:

fruits=("apple" "banana" "orange" "grape")

for fruit in "${fruits[@]}"
do
  echo "I like $fruit"
done

In this example, we have an array called fruits that contains four elements: "apple," "banana," "orange," and "grape." The for loop iterates over each element in the array, assigning it to the fruit variable. The code block the inside loop then echoes the statement "I like [fruit]" for each iteration.

The output of this script will be:

I like apple
I like banana
I like orange
I like grape

Using a For Loop with a Range of Values

Another way to use a Bash for loop is to iterate over a range of values. Bash provides the seq command to generate a sequence of numbers. Here's an example:

for i in $(seq 1 5)
do
  echo "Number: $i"
done

In this, example seq the 5 command generates a1 sequence of numbers from 1 to 5. The for loop iter over each numberates, assigning it to the i variable. The code block inside the loop then echoes the statement "Number: [i]" for each iteration.

The output of this script will be:

Number: 1
Number: 2
Number: 3
Number: 4
Number: 5

Using a For Loop with Command Output

In Bash, you can also use the output of a command as the list for a for loop. This can be useful when you want to iterate over the results of a command. Here's an example:

for file in $(ls *.txt)
do
  echo "Processing file: $file"
done

In this example, the ls *.txt command lists all the files in the current directory that have the ".txt" extension. The loop iterates for over each file, assigning its name to the file variable. The code block inside the loop then echoes the statement "Processing file: [file]" for each iteration.

You can replace the ls *.txt command with any other command that generates a list of values, depending on your requirements.

Modifying the Behavior of a For Loop

By default, a Bash for loop iterates over the elements in the list sequentially. However, you can modify its behavior using additional keywords and constructs.

Using the break Statement

The break statement allows you to exit the loop prematurely based on a certain condition. Here's an example:

for number in 1 2 3 4 5
do
  if [ $number -eq 3 ]
  then
      break
  fi
  echo "Number: $number"
done

In this example, the for loop iterates over the numbers 1 to 5. However, when the value of number becomes 3, the break statement is executed, and the loop terminates. As a result, only the numbers 1 and 2 are printed.

The output of this script will be:

Number: 1
Number: 2

Using the continue Statement

The continue statement allows you to skip the rest of the code block in the current iteration and move on to the next iteration. Here's an example:

for number in 1 2 3 4 5
do
  if [ $number -eq 3 ]
  then
      continue
  fi
  echo "Number: $number"
done

In this example, when the value of number becomes 3, the continue statement is executed, and the code block is skipped for that iteration. As a result, the number 3 is not printed.

The output of this script will be:

Number: 1
Number: 2
Number: 4
Number: 5

Using the in Keyword with a Command

Instead of specifying a list explicitly, you can use a command inside the in keyword generate to the list dynamically. Here's an example:

for user in $(cat users.txt)
do
  echo "Processing user: $user"
done

In this example, the cat users.txt command reads the contents of the users.txt file, which contains a list of usernames. The for loop iterates over each username, assigning it to the user variable. The code block inside the loop then echoes the statement "Processing user: [user]" for each iteration.

You can replace the cat users.txt command with any other command that generates a list of values, depending on your requirements.

bash for loop 1 to 10

This will output each number from 1 to 10 on a new line.

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

Alternatively, you can also use the seq command to create a sequence of numbers and loop over it:

for i in $(seq 1 10); do
  echo "$i"
done

Both of these methods are safe and won't produce any harmful or unethical output.

Conclusion

In this article, we explored the Bash for loop, an essential construct in Bash scripting. We discussed its syntax, usage with arrays, ranges, and command outputs. We also learned how to modify the behavior of a for loop using the break and continue statements. Armed with this knowledge, you can now leverage the power of for loops to automate repetitive tasks and process data efficiently in your Bash scripts. Experiment with different examples and explore the vast possibilities that the Bash for loop

Further Reading


bashBash For LoopBash loop

📝 Written by Haikel Fazzani

📝 Latest Blog Posts

Stay updated on the world of software development with our fresh articles and expert insights.