Looping becomes a handy tool for the execution of a large list of commands repeatedly with different variations. In Linux, the shell scripting allows four types of loops such as “for”, “while”, “until”, and “select”. These loops are used for executing the same line of code again and again until the defined conditions become true. Apart from that, the “for” and “while” looping also allows reading the file content line by line.
This guide pens down all possible ways to loop over the lines of a file in a bash script:
Method 1: Using “for” Loop
The “for” loop works on the number of items defined in the list. The list may contain a finite or infinite number of items. The “for” loop iterates for each item in the list that is stored in the user-defined variable.
The “for” loop is widely used for its simple syntax that is written here:
for var in <list> do <commands> done
The syntax contains the following parameters:
- for: The main keyword that represents the “for” loop.
- var: Specifies a variable that stores each item mentioned in the list.
- list: Denotes the series of numeric values or strings.
- commands: Show the set of commands that will execute repeatedly for each item.
- do: Represents the starting of the “for” loop.
- done: Identifies the ending block of the “for” loop.
In this method, an example is shown to illustrate how each line of a file can be read or displayed in the terminal using the “for” loop.
Read the File Content
The targeted file “itslinuxfoss.txt” contains the following lines of data whose content will be iterated through the “for” loop as line by line:
$ cat itslinuxfoss.txt
Create a “for” Loop in Script
The “home” directory contains a bash script opened in the “nano” text editor in which a “for” loop is created:
$ nano code.sh
#!/bin/bash Filename="itslinuxfoss.txt" Each_Line=$(cat $Filename) for Line in $Each_Line do echo "$Line" done
The above “for” loop has following parameters:
- Filename variable is declared in that stores the targeted file “itslinuxfoss.txt”.
- Each_Line is the second variable in which the “cat” command is declared as another variable that reads the variable “$Filename” file content.
- The “Line” denotes the iterator that works on the “$Each_Line” variable to display the content of “itslinuxfoss” one by one using the “for” loop.
- At this end of the script, the do and done blocks enclosed the “echo” statement that prints the “$Line” value:
Press (Ctrl+S) to save and (Ctrl+X) to exit the script.
Execute the script and check the “for” loop output in this way:
Each line of the “itslinuxfoss” file is printed in the terminal.
Method 2: Using “while” Loop
The simple “while” loop performs the command execution for an unknown number of times until the defined condition becomes true. Its most common command usage is to read the file line by line, or data stream in the form of a ”while” loop.
The basic syntax of a while loop to read each line of a file is typed below:
while read -r linedo echo "$line" done < input.file
The “while” loop syntax is illustrated here:
- while: Represents the “while” loop, i.e., main keyword.
- read: Read the file content.
- $line: Variable that stores the input file content.
- -r: Optional flag that avoids the backslash escapes “\” from being interpreted.
- < input.file: Redirect the “input.file” to the “read” command that manages the loop. The loop will iterate till the last line of “input.file”.
- do & done: Displays the start and ending block of the loop.
Example 1: Read File Line by Line
The following example shows how the “while” loop displays file content line by line.
Using a “while” Loop In Script
Open an already existing bash script “program.sh” in which a “while” is used:
$ nano program.sh
#!/bin/bash while read line do echo "$line" done < itslinuxfoss.txt
In the above “while” loop the input file “itslinuxfoss.txt” is redirected to the “read” command with “<(input redirection)” whose each line will be read:
Run the “program.sh” script by specifying its name in the terminal:
Each line of the “itslinuxfoss.txt” file has been printed on the terminal.
Example 2: Print the Null Strings
By default, the “while” loop trims the spaces placed at the specific position in the text file. To keep the spaces, use “Internal Field Separator(IFS)” that sets the null strings trim while using a simple “while” loop.
The “IFS” with an equal sign is placed before the “read” command in this way:
#!/bin/bash while IFS= read line do echo "$line" done < itslinuxfoss.txt
The above script is the “Program.sh” defined in the (Example 1)“read-while” loop with an additional “IFS=”:
After saving, execute the “program.sh” script again and see the results:
The null string before the “Kernel” remains the same and has been printed in the terminal.
To loop over the lines of a file, the “for” and the “while” loops can be used in Linux. The “for” iterates until the main part of it executes for all the mentioned list items. On the other hand, the “while” loop reads the file content till the last line of the input file.
This guide has illustrated all possible aspects to loop over the lines of a file in bash script.