Home » Linux » Shell » Bash For Loop

Bash For Loop [With Examples]

The for loop is a handy tool when writing Bash scripts for repeating a task for a number of files, records, or other values.

The for statement will iterate over a list of values, performing tasks on each one until all items have been processed.

Usage cases include looping over the files in a directory, the lines in a text file, or the output of a database query.

Bash For Loop Syntax

for VARIABLE in LIST
do
    COMMANDS
done

Where:

  • VARIABLE is the variable name that will be assigned to each item in the LIST when it is processed by the COMMANDS
  • LIST can take a number of forms. See below for examples

Examples of Bash For Loop

for loops can iterate over predefined lists or lists generated from the output of other scripts or command-line applications.

Iterating Over a Range of Numbers With a for Loop

Processing each number in the range (incrementing by 1) using {START…END}:

for num in {0..10}
do 
    echo "I'm counting to 10 and I'm up to $num"
done

Note that:

  • Each item in the list is given the variable name num.
    • These are accessed later in the script using $num.
    • Iterating over ranges of numbers is only supported in Bash version 3 and up.
  • The numbers to be iterated over begin with the number at the beginning of the curly braces and run to the number at the end, in increments of 1

Stepping over numbers and incrementing by 3 – so that only multiples of 3 are processed on – using {START…END…INCREMENT}:

for num in {0..15..3}
do 
    echo "$num is a multiple of 3!"
done

Note that:

  • A third number is added to the expression in the curly braces.
    • This will operate on numbers from 0 to 15 in increments of 3
  • Iterating over ranges with a custom increment is only supported in Bash version 4 and up.

Iterating Over a List of Strings With a for Loop

You can also iterate over a list of string values:

for car in Ford, Holden, Honda, BMW, Ferrari
do
    echo "I wish I could drive a $car"
done

Iterating Over an Array With a for Loop

An array can be declared as a variable before iterating over the values in it:

FRUITS=('Apple' 'Pear' 'Orange' 'Strawberry')

for fruit in "${FRUITS[@]}"
do
    echo "$fruit is delicious"
done

Iterating Over Files (And other Output) With a for Loop

You can also loop over the output of other command-line commands:

for f in *
do
    echo "Filename is $f"
done

Break

The break statement will exit the loop at a given point:

for num in {0..15..3}
do
    echo "$num is a multiple of 3!"
    if [[ "$num" == '9' ]]
    then
        break
    fi
done
echo 'Finished counting at 9!'

Continue

The continue statement will stop executing the code in the current iteration of the loop (skipping it entirely) and continue on to the next iteration:

for num in {0..15..3}
do
    if [[ "$num" == '6' ]]
    then
        continue
    fi
    echo "$num is a multiple of 3!"
done
echo 'The number 6 was skipped!'

Conclusion

Using loops while writing scripts will save you time and allow you to write flexible scripts that can automate processing files and data without manual data entry.

Check out our other articles for more Bash scripting tips!

SHARE:
Photo of author
Author
I'm Brad, and I'm nearing 20 years of experience with Linux. I've worked in just about every IT role there is before taking the leap into software development. Currently, I'm building desktop and web-based solutions with NodeJS and PHP hosted on Linux infrastructure. Visit my blog or find me on Twitter to see what I'm up to.

Leave a Comment