Welcome to the spoken tutorial on Nested for loop in BASH.
In this tutorial, we will learn Nested for loop with the help of an example.
To record this tutorial, I am using Ubuntu Linux 12.04 Operating System and GNU BASH version 4.1.10
Please note, GNU Bash version 4 or above is recommended for practice.
To learn this tutorial, you should be familiar with loops in Bash.
For relevant tutorials, please visit our website which is as shown- http://spoken-tutorial.org
Let us start with an introduction to nested loop.
A loop within a loop is known as nested loop.
Let us see the syntax. Outer for loop expression 1, 2, 3;
inner for loop expression 1, 2, 3;
statement 1, statement 2
closing inner for loop, closing outer for loop.
Let us see an example on nested for loop.
First, let us go through the directory structure.
Here is a directory on the Desktop named simple-nested-for. Let's open it.
We have sub-directories "test", "test2", "test3" and a Bash script.
In each sub-directory, there are multiple text files.
Now we will move on to our code.
This program displays all the files within each sub-directory.
Please note that the same can be achieved with a single line command ls -1 (hyphen one) -R(hyphen R) test*(test asterisk).
But we will do it using a for loop.
Note that the name of our Bash script is nested-(hyphen)for dot sh.
This is our shebang line.
This is the outer for loop.
This for loop will check for directories starting with the name test.
First echo line will display the sub-directories name.
The second echo line will create a blank line.
This is the inner for loop. It will check for the files present within the directories.
"ls" displays the directory content.
-1 (hyphen one) is used to list one file per line.
Here, we list the files. done ends the inner for loop.
This command prints a horizontal line after completion of every cycle of outer for-loop.
done ends the outer for loop.
Let us execute the program.Open the terminal by pressing ctrl+alt+t keys simultaneously on your keyboard.
Now, let us go to the directory where our Bash script is.
It is on the Desktop.
Type: cd Desktop. Let us go in the folder simple-(Hyphen)nested-(Hyphen)for
Press Enter.
Type: chmod plus +x nested-(Hyphen)for dot sh
Press Enter.
Type: dot slash nested-(Hyphen)for dot sh
Press Enter.The output is displayed. It shows files in test directory, files in test2 directory and files in test3 directory.
This brings us to the end of this tutorial.
Let us summarize. In this tutorial, we learned Nested for loop.
As an assignment:
Retype: nested (hyphen)-for dot sh bash script using nested while loop.
Save your program with the name 'nested-(hyphen)while dot sh'.
Watch the video available at the link shown below. It summarizes the Spoken-Tutorial project.
If you do not have good bandwidth, you can download and watch it.
The Spoken Tutorial Project team: Conducts workshops using spoken tutorials. Gives certificates to those who pass an online test.
For more details, please write to contact@spoken-tutorial.org
Spoken Tutorial project is a part of the Talk to a Teacher project. It is supported by the National Mission on Education through ICT, MHRD, Government of India.
More information on this mission is available at: http://spoken-tutorial.org/NMEICT-Intro
The script has been contributed by FOSSEE and Spoken-tutorial teams.
This is Ashwini Patil from IIT Bombay. Thank You for joining.