Bash for loop examples

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 for loops through examples on Linux
Executing a Bash for loop on Linux
Executing a Bash for loop on Linux
Software Requirements and Linux Command Line Conventions
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.

  1. To get a basic idea of how for loops work, examine the example below where each integer is individually echoed:
    $ for i in 1 2 3 4 5; do echo $i; done

    In this case, the i variable 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 of for loops.

  2. Let’s try something a little more practical. This example will output the first line of each .txt file it finds in the present working directory.
    $ for i in $(ls *.txt); do cat "$i" | head -n1; done

    The ls command in this example will list all text file in the directory, and each name will be stored inside the i variable, one file for each loop that the for loop will run through. After we have the file name, we run the cat and head commands to retrieve the first line of the file.

  3. A Bash script that will execute a command 10 times, while incrementing the variable with each loop:
    for i in {1..10}
       echo "Hello World number $i"
  4. It’s also possible to increment values using this format:
    for (( i=1; i<=10; i++ ))
       echo "Hello World number $i"
  5. A for loop can also be infinite, which is useful if you want a script to continue running until an interrupt from ctrl + c or kill, etc.
    for (( ; ; ))
       echo "An infinite running for loop"
       sleep 1
  6. As mentioned earlier, for loops are especially useful for the time and keystrokes they save us. Let’s look at a practical example where we use a for loop to SSH and execute the df -h command on three different servers.
    for s in server1 server2 server3
        ssh linuxconfig@${s} "df -h"

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.

Comments and Discussions
Linux Forum