Bash provides a lot of useful programming functionalities. for loop
is one of the most useful of them. We can use for loop for iterative jobs. Linux system administrators generally use for loop to iterate over files and folder. In this tutorial, we will look at how to use for
loop to iterate over files and directories in Linux. This example can be used any of Linux distribution which uses bash as shell-like Ubuntu, CentOS, RedHat, Fedora, Debian, Kali, Mint, etc. This mechanism is named as for each
some programming languages where a list is iterated over.
For Loop Syntax
Bash provides different usages and syntax for the for loop
but in general following syntax is used .
for F in ITEM1 ITEM2 ...; do
CODE
done
for
is the keyword which is used to create loops in bashF
is the element or item which is populated in each step from ITEMSITEM1
,ITEM2
,ITEM3
, … are items we want to iterate this can be also some list which contains multiple items- CODE is the implementation part which is executed in each loop
Numeric Syntax
One of the most used Numeric
syntax. We will provide numbers as a list and iterate over given list. Every number in the list will be iterated inside the for loop one by one. Every item will be assigned to the variable named $VAR
like below and this variable named $VAR can be used inside the for loop book.
for VAR in 1 2 3 4 .. N
do
command1 $VAR
command2
command3
...
commandN
done
In numeric syntax, it is very similar to the general syntax where we provide a collection of numbers.
Given File List Syntax
We will provide the files as a list and use them in each iteration. In this type, we will iterate over the list of files like file1
, file2
, file3
etc.
for VAR in file1 file2 file3 file3 ... filen
do
command1 $VAR
command2
command3
...
commandN
done
Command Output Syntax
We can use bash commands output as items for iterate. In this syntax, we expect that the $(BASH_COMMAND)
will return a list where we will iterate over this list.
for VAR in $(BASH_COMMAND)
do
command1 $VAR
command2
command3
...
commandN
done
Loop Over Given File Names
The simplest usage for for
loop is over given file names. We will provide the file files by separating them with spaces. In this example, we will provide file names a
, b
and c
and then print them with some string.
for f in "a" "b" "c";
do
echo Processing $f ;
done;

Loop Over Listed File Names
What can we do if there are thousands of files to be a loop in a directory. We need a more dynamic way to provide file names. We can use ls
command in order to provide file names as a list without typing one by one.
for f in $(ls);
do
echo Processing $f ;
done;

Loop Over Specified File Extensions
Some times we may need to work on specific file extensions. We can specify the file extension we want to loop with for
loop. In this example, we will print encoding types of files with *.txt
extension.
for f in *.txt;
do
file $f ;
done;

Loop Over Files Reading From Text File
Files names can be stored in a text file line by line. We can read file names from the specified text file and use in a for
loop. In this example, we will read the following text file and loop over lines. Our file name is filenames.txt
a b c d
$ for f in $(cat filenames.txt);
do
echo Processing $f ;
done;

C Like For Loop
Up to now, we have learned the basic syntax of for loop. There is also more formal for loop which is the same as C Programming
language. We need to provide start condition, iterate operation and end condition.
for ((START_CONDITION;ITERATE_OPERATION;END_CONDITION))
do
COMMAND1
COMMAND2
...
COMMANDN
done
In this example, we will use echo
command to print from 1
to 5
with this for loop syntax.
#!/bin/bash
for (( c=1; c<=10; c++ ))
do
echo "Loop $c times"
done

Infinite Loop
In some cases, we may need infinite loops. The infinite loop will never end except its process is killed. We will use C like for loop in order to create an infinite loop.
#!/bin/bash
for (( ; ; ))
do
echo "This will run forever"
done

Conditional Break with exit
During for loop, we may need to exit for given conditions if they occur. exit
keyword can be used to break the iteration. In this example, we will check and if the current value of c
can be divided into 6
we will end the loop.
#!/bin/bash
for (( c=1; c<=10; c++ ))
do
if(($c%6 == 0))
then
exit
fi
echo "Loop $c times"
done
The terminal will be closed when the variable $c is equal to the 6 where the if will be true and exit
statement will be executed.
Skip To Next Step with continue
In some cases, we may need to skip the current iteration but resume to the loop. We can use continue
keyword which will step over to the next iteration. In this example, we will continue if the $c
variable can be divided with 3.
#!/bin/bash
for (( c=1; c<=10; c++ ))
do
if(($c%3 == 0))
then
continue
fi
echo "Loop $c times"
done
