For Loop in Bash

2 minute read

The for loop in Bash is a powerful and versatile construct that allows you to iterate over a sequence of values or elements. It’s an essential tool for automating repetitive tasks and handling data processing efficiently. In this tutorial, we will explore the syntax and usage of the for loop in Bash, accompanied by practical examples.

Basic Syntax of the for Loop

The basic syntax of the for loop in Bash is as follows:

for variable in sequence
do
  # Commands to be executed for each iteration
done

The variable takes on the values from the sequence during each iteration of the loop. The commands within the loop block are executed for each value of the variable.

Examples of Using the for Loop

Iterating Over Numbers

#!/bin/bash

echo "Counting from 1 to 5 using a for loop:"

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

In this example, the for loop iterates over the sequence of numbers from 1 to 5 and prints each number on a new line.

Iterating Over an Array

#!/bin/bash

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

echo "List of colors:"

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

Here, the for loop iterates over the elements of an array (colors) and prints each color on a new line.

Generating File Names

#!/bin/bash

echo "Creating files using a for loop:"

for i in {1..3}
do
  touch "file_$i.txt"
  echo "Created file_$i.txt"
done

This script demonstrates how to use a for loop to create multiple files with names like file_1.txt, file_2.txt, and file_3.txt.

Iterating Over Command Output

#!/bin/bash

echo "Listing files in the current directory using a for loop:"

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

In this example, the for loop iterates over the output of the ls command, listing all files in the current directory.

Using the seq Command for Numeric Sequences

#!/bin/bash

echo "Counting from 1 to 5 using the seq command:"

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

Here, the for loop utilizes the seq command to generate a sequence of numbers from 1 to 5.

Conclusion

The for loop in Bash is a versatile tool for automating tasks that involve iterating over a sequence of values or elements. Whether working with numbers, arrays, file names, or command output, the for loop simplifies the process of handling repetitive tasks in your scripts.

Now equipped with the knowledge from this tutorial, you can leverage the for loop to streamline your Bash scripting, making your code more efficient and readable.


Frequently Asked Questions

  1. Can I use variables other than numbers in a for loop?
    Yes, the for loop can iterate over arrays, strings, or any sequence of values.

  2. Is it possible to nest for loops in Bash?
    Yes, you can nest for loops to create more complex iteration structures.

  3. How do I iterate over a range of numbers in reverse order?
    Use the seq command with the -r option: $(seq -r 5 1) for counting from 5 to 1.

  4. Can I use the for loop to iterate over files in a specific directory?
    Yes, use the for loop with a command that lists files in the desired directory, such as $(ls /path/to/directory).

  5. Are there alternative ways to create sequences of numbers in Bash without using the for loop?
    Yes, you can use the seq command or curly braces {} for numeric sequences.

Updated: