“For” loops are very commonly used in all the programming languages. Similarly, Bash also has a dedicated syntax for making use of the “For” loop. The basic purpose of using this loop is to iterate through arrays which can also lead to other complex calculations. Therefore, in this article, we will be talking about the three different scenarios in which you can use the “For” loop for iterating through an array.
Method of using “For” Loop for printing all the Elements of an Array:
For printing all the elements of an array on the terminal by making use of the “For” loop, we will have to perform the following steps:
We will first create a Bash file with the name ForLoop.sh. However, you can also have any other name of your choice. A Bash file can simply be created by typing in the .sh extension after an ordinary document name. We have created this file in the Home folder by right-clicking on an empty space in it and then choosing the New Document option from the cascading menu and then we selected the Empty Document option from the sub-cascading menu. This file is shown in the image below:
Now type the script shown in the following image in your newly created bash file. This script declares an array variable named writers and assigns it five values i.e. John, Leena, Tom, Henry, and Drake. Then it has a “For” loop which has an iterator “i”. The purpose of this loop is to iterate through every element of this array and print them all one by one. Whenever the “$” symbol is typed before an array index, then it implies that we are trying to access the value of that particular index. Also, the “@” symbol enclosed within the square brackets after the name of the array represents that we are trying to refer to the iterator of this loop which in this case is “i”. In Bash scripting, whatever we want to do inside a “For” loop is enclosed within the “do” and “done” keywords. Since in this scenario, we simply want to print all the elements of an array on the terminal, therefore, we have used the echo command followed by a “$” symbol and the iterator that we specified after the “for” keyword.
Once you have typed this script, just save your file and close it. Now launch the terminal in Ubuntu 20.04 and type the following command in it to execute the ForLoop.sh file:
As soon as this file will execute successfully, you will be able to see all the elements of the writers’ array on your terminal:
Method of using “For” Loop for writing all Elements to a Text File:
For writing all the elements of an array to a text file using the “For” loop, you will have to perform the following steps:
In this method, we will just have to slightly modify the script used in the method above. Open the ForLoop.sh file again and make the modifications shown in the image below in your Bash script. Instead of printing the elements on the terminal, we have simply written them to a text file named Writers.txt by making use of the echo command followed by the”>>” symbol.
Now save this file and close it. After doing this, launch the terminal and run the Bash script that you have just modified in the same manner as explained above. As soon as this Bash script will run, you will notice that a new text file named Writers.txt has been created in your Home directory. For viewing the contents of this text file, type the following command in your terminal:
The output of this command will verify that the “For” loop has successfully iterated through the writers’ array as all of its elements have been correctly written to the Writers.txt file.
Method of using “For” Loop for Comparing Array Elements with a Given String:
For comparing the array elements with a given string by making use of the “For” loop, you will have to proceed as follows:
Again, for this method, we will modify the script that we have written for our very first scenario. For doing that, you need to open the ForLoop.sh file again and make the modifications shown in the image below. Our goal was to check if the value of any index of the writers’ array matches with our string “Leena”. If it does, then we wanted to print “Hi Leena”, otherwise, for all the other indexes, we simply wanted to print their values. For achieving this goal, we made use of the “if” statement within the “do-done” block. We have simply compared the value of every ith index with our string “Leena”. As soon as a match is found, we will print “Hi Leena” by making use of the echo command preceded by the “then” keyword. The “if” statement is always concluded with the “fi” keyword in Bash scripting. Moreover, for the rest of all the indexes, we have simply printed their values by making use of the echo command outside the “if-fi” block.
After making these changes, save your Bash file and close it. Now launch the terminal and execute this newly modified Bash file. The output of this Bash script will be sufficient to verify that the comparison has successfully taken place with the “For” loop combined with an “if” statement.
This article gives you a brief overview of using the “For” loop for iterating through an array. You can also make use of this loop for performing other programmatically complex calculations.