This comprehensive Linux guide expects that you run the following commands as root user but if you decide to run the commands as a different user then ensure that the user has sudo access and that you precede each of the privileged commands with sudo

In computer science a for loop is a programming language statement which allows code to be repeatedly executed. A for loop is classified as an iteration statement. Unlike many other kinds of loops, such as the while loop, the for loop is often distinguished by an explicit loop counter or loop variable. This allows the body of the for loop (the code that is being repeatedly executed) to know about the sequencing of each iteration. For loops are also typically used when the number of iterations is known before entering the loop. For loops are the shorthand way to make loops when the number of iterations is known.

General Syntax

Following is the general syntax of for loop for bash programming.

for variable in <list>
do
   <commands>
done

The list can be a series of strings separated by spaces, a range of numbers, output of a command, an array, and so on. The for loop will take each item in the list, assign that item as the value of the variable, execute the commands between do and done then go back to the top, grab the next item in the list and repeat over.

The for loop can also be used to take the list as an input from a file or multiple files.

for variable in file1 file2 file3
do
	<commands>
done

The for loop can also take the values of the loop variable from the output of another command. The output of that command will be considered as the input for the loop variable.

for variable in $(Command-Here)
do
	<commands>
done

You can also use the 3-Expression Syntax for the for loop similar to java and C++ programming languages.

for (( EXP1; EXP2; EXP3 ))
do
	<commands>
done

Examples Of for Loop Over Numbers

Followings are some examples of using for loop over numeric values.

Number Ranges

The for loop can be used over the range of numeric values. The loop will take the values from that range with an increment after each iteration.

/tmp/loop.sh
#!/bin/bash
for i in {1..5}
do
   echo "Welcome to CODESPOSTS $i times"
done

In this example, the loop will start the value of i with 1, and then increment 1 after each iteration.

root@codesposts:~$ bash loop.sh

Welcome to CODESPOSTS 1 times
Welcome to CODESPOSTS 2 times
Welcome to CODESPOSTS 3 times
Welcome to CODESPOSTS 4 times
Welcome to CODESPOSTS 5 times

Specific Increment

You can specify the increment to the for loop yourself. The default increment is “1” as you can see in the previous example.

/tmp/loop.sh
#!/bin/bash
for i in {1..10..2}
do
   echo "Welcome to CODESPOSTS $i times"
done

The first and second argument represents the initial and final values. The third argument in the loop statement represents the increment.

root@codesposts:~$ bash loop.sh

Welcome to CODESPOSTS 1 times
Welcome to CODESPOSTS 3 times
Welcome to CODESPOSTS 5 times
Welcome to CODESPOSTS 7 times
Welcome to CODESPOSTS 9 times

3-Expression Syntax

You can also use 3-Expression Syntax for a range of numbers too. See the example below:

/tmp/loop.sh
#!/bin/bash
for (( i=1; i<=5; i++ ))
do  
   echo "Welcome to CODESPOSTS $i times"
done

The first expression provides the initial value for the variable, the second expression provides the condition for which the loop will end, and the third expression provides the increment after each iteration.

root@codesposts:~$ bash loop.sh

Welcome to CODESPOSTS 1 times
Welcome to CODESPOSTS 2 times
Welcome to CODESPOSTS 3 times
Welcome to CODESPOSTS 4 times
Welcome to CODESPOSTS 5 times

List Of Numbers

You can use a list of numbers as the input for the for loop.

/tmp/loop.sh
#!/bin/bash
for i in 1 2 3 4 5
do
   echo "Welcome to CODESPOSTS $i times"
done

This will take the values mentioned in the list one after each iteration.

root@codesposts:~$ bash loop.sh

Welcome to CODESPOSTS 1 times
Welcome to CODESPOSTS 2 times
Welcome to CODESPOSTS 3 times
Welcome to CODESPOSTS 4 times
Welcome to CODESPOSTS 5 times

Examples Of for Loop Over Strings

The bash for loop can also be used over the list of String values.

/tmp/loop.sh
#!/bin/bash
names='Muhammad Mubeen Tahir'
for name in $names
do
echo $name
done

This bash script will take the names one by one as the value for the variable name and then execute the commands written in the body of the loop and then take the next name in the list.

root@codesposts:~$ bash loop.sh

Muhammad
Mubeen
Tahir

Break Statement With for Loop

The break statement tells Bash to leave the loop straight away. It may be that there is a normal situation that should cause the loop to end but there are also exceptional situations in which it should end as well. For instance, maybe we are copying files but if the free disk space get’s below a certain level we should stop copying.

/tmp/loop.sh
#!/bin/bash
for i in 1 2 3 4 5
do
   if[i == 3]
   then
         break
   fi
         echo "Welcome to CODESPOSTS $i times"
done

When the value of variable i reaches to 3, the if statement will execute the break statement, and the loop will exit without going to the next iteration.

root@codesposts:~$ bash loop.sh

Welcome to CODESPOSTS 1 times
Welcome to CODESPOSTS 2 times

Continue Statement With for Loop

The continue statement tells Bash to stop running through this iteration of the loop and begin the next iteration. Sometimes there are circumstances that stop us from going any further. For instance, maybe we are using the loop to process a series of files but if we happen upon a file which we don’t have the read permission for we should not try to process it.

/tmp/loop.sh
#!/bin/bash
for i in {1..5}; 
do
  if [[ "$i" == '2' ]]; 
  then
    continue
  fi
  echo "Number: $i"
done

root@codesposts:~$ bash loop.sh

Number: 1
Number: 3
Number: 4
Number: 5

for Loop With Files

We can also use files to take input for the for loop. See the example below.

/tmp/loop.sh
#!/bin/bash
for file in ~/path/*.md
do
	echo $file
done