With a Bash for loop on a Linux system, it is possible to continue executing a set of instructions for a certain number of files or until a particular condition is met.
Loops can be used in Bash scripting or directly from the command line. A for loop is useful because it can repeatedly execute code for a certain number of times or for a certain number of files. This saves us keystrokes and time as it is not uncommon for a loop to execute hundreds of times, depending on your scenario.
In this tutorial, you will see how to use Bash for loops through bash scripting and command line examples on Linux.
In this tutorial you will learn:
- How to use Bash
forloops through examples on Linux

| Category | Requirements, Conventions or Software Version Used |
|---|---|
| System | Ubuntu Linux |
| Software | Bash shell (installed by default) |
| Other | Privileged access to your Linux system as root or via the sudo command. |
| Conventions |
# – requires given linux commands to be executed with root privileges either directly as a root user or by use of sudo command$ – requires given linux commands to be executed as a regular non-privileged user |
Bash for loop examples on Linux
Below you will find multiple
for loop examples that you can copy and paste to your own system. Feel free to test them out and adapt them to your own needs. We will explain each one so you can understand how these loops work and you will eventually be able to write your own.
- To get a basic idea of how
forloops work, examine the example below where each integer is individually echoed:$ for i in 1 2 3 4 5; do echo $i; done 1 2 3 4 5
In this case, the
ivariable is first set to 1, then 2, etc. On each loop, the current integer is echoed to terminal. This is a basic example and rather useless in a real scenario, but it illustrates the most basic concept offorloops. - Let’s try something a little more practical. This example will output the first line of each
.txtfile it finds in the present working directory.$ for i in $(ls *.txt); do cat "$i" | head -n1; done
The
lscommand in this example will list all text file in the directory, and each name will be stored inside theivariable, one file for each loop that theforloop will run through. After we have the file name, we run thecatandheadcommands to retrieve the first line of the file. - A Bash script that will execute a command 10 times, while incrementing the variable with each loop:
#!/bin/bash for i in {1..10} do echo "Hello World number $i" done - It’s also possible to increment values using this format:
#!/bin/bash for (( i=1; i<=10; i++ )) do echo "Hello World number $i" done
- A
forloop can also be infinite, which is useful if you want a script to continue running until an interrupt fromctrl + corkill, etc.#!/bin/bash for (( ; ; )) do echo "An infinite running for loop" sleep 1 done
- As mentioned earlier,
forloops are especially useful for the time and keystrokes they save us. Let’s look at a practical example where we use aforloop to SSH and execute thedf -hcommand on three different servers.#!/bin/bash for s in server1 server2 server3 do ssh linuxconfig@${s} "df -h" done
Closing Thoughts
For Linux admins or power users, knowing how to use the for loop in Bash is essential for automating administration tasks and file manipulation. The examples here only scrape the surface of what is possible, but we have shown you the syntax and capabilities of for loops. Now you can adapt these examples into your own Bash scripts or command line one-liners.