How to Write a BASH 'for' Loop

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

What to Know

  • In a Bash for loop, all statements between do and done are performed once for every item in a list or number range.
  • With a big list, use in {list} to loop between a start and end point. Use ellipsis to iterate a full number range, e.g., for number in {1..10}.
  • To skip certain numbers, add a third number to the range. For example, use {0..100..10} to only list every 10th number.

In a programming or scripting language, Bash offers several ways to repeat code—a process called looping—where a for loop repeats a certain section of the code. This allows a series of commands to run until a particular condition is met. In this guide, we show you how to write a Bash for Loop.

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.

Consider a simple example script titled

for number in 1 2 3 4 5
echo $number
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 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:

for number in {1..10}
echo "$number "
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:

for number in {0..100..10}
echo "$number "
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:

for file in ./*.mp3
mpg -w ./wavs/"${file}".wav "$file"

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.

Was this page helpful?