How To Write A BASH 'for' Loop

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

Person using GNU nano and a bash for loop

 Lifewire / Maddy Price

Scripting languages such as BASH feature similar programming constructs as other languages. For example, you can use import parameters to get input from the keyboard and store them as variables. You can then get the script to perform a certain action based on the value of the input parameters.

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 also known as looping. A for loop repeats a certain section of the code over and over. They're useful so that a series of commands can keep running until a particular condition is met, after which they'll stop.

How to Loop Through a List

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 most 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 our 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 them one by one to the screen:

loop example in a bash script

How to Loop Between a Start and End Point

The trouble with our example loop.sh script is that if you want to process a bigger list — e.g., 1 to 500 — it would take ages to type all the numbers in the first place.

This brings us to the second example which shows how to specify a start and end point:

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

The rules are basically 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 (i.e. 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 {} basically denotes 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 as follows:

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 end point, so now we'll look at how to skip numbers in the range.

Imagine you want to loop between 0 and 100 but only show every tenth number. The following script obtains this output:

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

The rules are basically 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 can do more than iterate lists of numbers. You can actually 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 your package manager first.