How to Write a BASH 'for' Loop

How to use the BASH 'for' loop in shell scripts

A key part of any programming and scripting language is the ability to run the same piece of code again and again. Bash offers several ways to repeat code—a process called looping. A for loop repeats a certain section of the code. These are useful so that a series of commands run until a particular condition is met, after which the commands stop.

How to Loop Through a List

Scripting languages such as Bash feature similar programming constructs as other languages. For example, import parameters get input from the keyboard and store these inputs as variables, which then perform a certain action based on the value of the input parameters.

Person using GNU nano and a bash for loop
Lifewire / Maddy Price

Consider a simple example script titled loop.sh:

#!/bin/bash
for number in 1 2 3 4 5
do
echo $number
done
exit 0

The Bash way of using for loops is somewhat different from the way other programming and scripting languages handle for loops. Let's break the script down.

In a BASH for loop, all the statements between do and done are performed once for every item in the list. In this example, the list is everything that comes after the word in—the numbers 1 2 3 4 5.

Each time the loop iterates, the next value in the list is inserted into the variable specified after the word for. In the above loop, the variable is called number.

The echo statement displays information to the screen. Therefore, this example takes the numbers 1 through 5 and outputs each number one by one to the screen:

loop example in a bash script

How to Loop Between a Start and End Point

The trouble with this loop.sh script is that if you want to process a bigger list (for example, 1 to 500), it would take ages to type all the numbers. Instead, specify a start and endpoint:

#!/bin/bash
for number in {1..10}
do
echo "$number "
done
exit 0

The rules are the same. The values after the word in make up the list to iterate through, and each value in the list is placed in the variable (that is, number), and each time the loop iterates, the statements between do and done are performed.

The main difference is the way the list is formed. The curly brackets denote a range, and the range, in this case, is 1 to 10 (the two dots separate the start and end of a range).

This example, therefore, runs through each number between 1 and 10 and outputs the number to the screen.

The same loop could have been written like this, with syntax identical to the first example:

for number in 1 2 3 4 5 6 7 8 9 10

How to Skip Numbers in a Range

The previous example showed how to loop between a start and endpoint. Here's how to skip numbers in the range.

For example, to loop between 0 and 100 but only show every tenth number, use the following script to obtain this output:

#!/bin/bash
for number in {0..100..10}
do
echo "$number "
done
exit 0

The rules are the same. There is a list, a variable, and a set of statements to be performed between do and done. The list this time looks like this: {0..100..10}.

The first number is 0 and the end number is 100. The third number (10) is the number of items in the list that it will skip.

The above example, therefore, displays the following output:

alternative bash loop output

A Practical Example

For loops do more than iterate lists of numbers. For example, use the output of other commands as the list.

The following example shows how to convert audio files from MP3 to WAV:

#!/bin/bash
for file in ./*.mp3
do
mpg -w ./wavs/"${file}".wav "$file"
done

The list in this example is every file with the .MP3 extension in the current folder, and the variable is a file.

The mpg command converts the MP3 file into WAV. However, you probably need to install this tool using a package manager first.