How to write for loop in Bash

Aug 01, 2023#bash

The for loop is a fundamental programming construct to execute a series of commands repeatedly for a predefined set of values or elements.

With the for loop, Bash scripts can efficiently iterate over lists, arrays, file contents, or numerical ranges, making it a powerful tool for automation and repetitive tasks.

The syntax is:

for <variable> in <list>; do
  <commands>
done

Where:

  • <variable> is the name of the loop iterator that takes on the values from the list.
  • <list> is a sequence of words, numbers, or other data items that the loop will iterate over.
  • <commands> are the commands that will be executed for each iteration of the loop. You can use continue or break to alter the execution of the loop based on certain conditions.

There are different ways to specify the list for a for loop in Bash, such as:

  1. Using seq command to generate a sequence of numbers and use the $() syntax to pass the output to the for loop. For example, this loop will print the numbers from 1 to 10:
for i in $(seq 1 10); do
  echo $i
done
  1. Using {start..end..step} syntax to generate a range of numbers with step size. For example, this loop will print the odd numbers from 1 to 9:
for i in {1..9..2}; do
  echo $i
done
  1. Using (( )) syntax to use a C-style expression. For example, this loop will print the numbers from 0 to 9:
for ((i = 0; i < 10; i++)); do
  echo $i
done
  1. Using a list of words or strings separated by spaces. You can simply write them after the in keyword in the for loop header. For example, this loop will print the words “hello”, “world” and “bash”:
for w in hello world bash; do
  echo $w
done
  1. Using an array variable. To iterate over the elements of an array called arr, you can use the syntax ${arr[@]} to expand the array into a list of words. For example, this loop will print the elements of the array:
arr=(apple banana cherry)
for e in ${arr[@]}; do
  echo $e
done
  1. Using the output of another command, such as find $somepath -type d, you can use the syntax $(command) to execute the command and pass its output to the for loop. For example, this loop will print the names of all directories in the current path:
for d in $(find . -type d); do
  echo $d
done

Using continue and break statements

These are two control flow statements that allow you to alter the execution of the loop based on certain conditions.

The continue statement skips the remaining commands in the current iteration of the loop and passes the control to the next iteration. For example, this loop will print only the even numbers from 1 to 10:

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

The break statement terminates the current loop and passes the control to the command that follows the loop. It is used to exit from a loop when a certain condition is met. For example, this loop will print the numbers from 1 to 5 and then exit:

for i in {1..10}; do
  if [[ $i -gt 5 ]]; then
    break
  fi
  echo $i
done