Whether you are a programming beginner or an experienced developer, you will agree with us on one thing – loops are among the basic concepts of many languages used in programming.
Do you need to repeatedly execute a line of commands until you meet a particular requirement? In that case, you can use a loop. It can save you a lot of time when you need to automate the execution of repetitive tasks.
If you use Bash, you should know that there are three ways to construct loops – for, until, and while loop.
In this article, we will focus on how to utilize the Bash for loop and what you should know about using it.
Basic Bash for Loop
Here is the basic form of the Bash for loop:
for element in [list] do [commands] done
For you to understand better, let’s make an example immediately:
for name in Tom Ben Mark Steven Peter do echo "Name: $name" done
We connected the “name” variable to the current item in the above example. You can use the strings of your preference in the list, as well as a number range and other items. In the command section, you set how you want the output to look.
The result of the above loop will be this:
Name: Tom Name: Ben Name: Mark Name: Steven Name: Peter
How to Use a Range of Numbers in a Loop
If you need to loop over a range of numbers, the easiest way is to use a sequence expression. The basic format is this one:
{STARTING_NUMBER..ENDING_NUMBER}
Let’s say that you need to iterate numbers from 1 to 5. Here is how your Bash for loop should look:
for i in {1..5} do echo "No: $i" done
The output will list the following:
No: 1 No: 2 No: 3 No: 4 No: 5
What happens if you do not need a sequence of numbers, but increments? Use the below line to pinpoint the increment you want to utilize.
{STARTING_NUMBER..ENDING_NUMBER..INCREMENT}
For example, we want all numbers from 3 to 15 in increments of 3. Here is what command we can use”
for i in {3..15..3} do echo "No: $i" done
The result will look like this:
No: 3 No: 6 No: 9 No: 12 No: 15
An Array of Elements
The best way to explain is once again via an example. Let’s say that we want to define a “movies” array and iterate over each item in that array.
Here is how the loop could look like:
MOVIES=('Titanic' 'The Shining' 'The Silence of the Lambs' 'Green Book' 'The Notebook') For movie in "${MOVIES[@]}"; do echo "Movie: $movie" done
We used the first line to define the “MOVIES” array and then moved to set the loop. You probably assume the result, but here is how the output of the above loop should look:
Movie: Titanic Movie: The Shining Movie: The Silence of the Lambs Movie: Green Book Movie: The Notebook
C-Style Bash for Loop
Let’s take another step forward and check out how you can use the C-style for loop. Here is the basic syntax:
For ((INITIALIZE; CHECK; STEP)) do [commands] done
Upon the start of the loop, it executes the INITIALIZE section. The next in line is the CHECK part, where a false return value causes loop termination. If the CHECK part is true, the loop executes the commands and updates the STEP section.
Let’s try by setting the INITIALIZE to i=1. The CHECK section will test if i≤100. If the checking process returns true, the output will start with the current value of the variable “i” and increase it in increments of 1 (i++) as long as the CHECK value is true.
for ((i = 1; i <= 100; i++)); do echo "Number: $i" done
The output will return precisely 100 lines of output:
Number: 1 Number: 2 Number: 3 … Number: 99 Number: 100
That is where the loop will terminate because you set the termination if the value of “i” is higher than 100.
For demonstration purposes, we only ran the loop for ten numbers as you can see below.
Break Statement
If you want to achieve further control on the execution of the for loop, you can do so with a break statement. Its goal is to execute loop termination and then give the control to the following statement after the one that was terminated.
It sounds tricky, but the idea is to execute loop termination when it meets a particular requirement.
Let’s use our list of names again, but set the loop to terminate when it reaches Mark. Here is how that should look:
for name in Tom Ben Mark Steven Peter do if [[ "$name" == 'Mark' ]]; then break fi echo "Name: $name" done echo "No more names!"
You will get the following output:
Name: Tom Name: Ben No more names!
Continue Statement
Unlike the break statement, the continue allows you to move from one iteration within the loop to the next.
Let’s say we want to iterate all the names in our list, but skip Mark and continue with Steven. Here is the Bash for loop that works in that case:
for name in Tom Ben Mark Steven Peter do if [[ "$name" == 'Mark' ]]; then continue fi echo "Name: $name" done
We ordered the loop to skip Mark from the list, which we have done by terminating the loop when it reaches his name. We then told the loop to return to the next iteration and continue the loop.
If done right, the output will be the following:
Name: Tom Name: Ben Name: Steven Name: Peter
You can do this with numbers, arrays, and pretty much anything else; it just takes a bit of creativity.
Using Bash for Loop to Rename Files and Modify Their Extensions
Did you know that you can also use the Bash for Loop to rename multiple files at once? For example, let’s say you want to change all files with an underscore in a particular directory, and change it to a hyphen.
Here is the code:
for file in *\*; do mv "$file" "${file//_/ -}" done
The opening line is there to create a file list while the “mv” command is used for renaming files. The part inside the curly brackets specifies what you want to rename (underscore) with how you want to rename it (hyphen).
You can also do this if you want to switch the extension of the files in a particular folder.
The command will look like this:
for file in *.docx; do mv - "$file" "${file%.docx}.doc" done
Similar to the file renaming loop, you first create a list and then choose the extension to modify and how to modify it.
Now let’s say you want to find the files you just renamed. We have the guide for using the find command covered fully for you.
Conclusion
If you are a newbie programmer, it might take a bit of time to get ahold of how the Bash for loop works. However, it is not difficult, and you should master it in a matter of minutes. Go ahead and try some of the loops from this tutorial and it won’t take long before you start creating loops, too!
Thomas Hyde
Related posts
Popular Articles
Best Linux Distros for Developers and Programmers as of 2025
Linux might not be the preferred operating system of most regular users, but it’s definitely the go-to choice for the majority of developers and programmers. While other operating systems can also get the job done pretty well, Linux is a more specialized OS that was…
How to Install Pip on Ubuntu Linux
If you are a fan of using Python programming language, you can make your life easier by using Python Pip. It is a package management utility that allows you to install and manage Python software packages easily. Ubuntu doesn’t come with pre-installed Pip, but here…