Bash For Each Line in File

The programmers are familiar with the term “for loop”, a type of iteration utilized in the programming language to run applications multiple times. Bash scripting is a programming language where you can run multiple commands by executing the bash script. In the bash script, the “for” is utilized for iteration purposes and can be used to read the lines of the file. The lines read by the “for” loop can be used to serve a specific purpose, i.e., copying or printing the content line by line. 

This post will address the explanation of the Bash for each line in a file with the help of suitable examples: 

Bash For Each Line in File

In bash, users can utilize the “for loop” to read all the lines of the file as well as to copy all the text of the file to another. To understand this, first, we will create a file using the nano text editor:

$ nano mybash_file.txt

When the file is created and opened, type some text lines:

Welcome_to_ItsLinuxFoss.
My_name_is_Maddox.
This_tutorial_is_about_the_usage_of_the_“For_Each_Line”.

Exit the nano text editor by saving the file. After this, both examples will be explained using the above file. 

Example 1: Read All the Lines of the Text File

The “for loop” can be utilized to read all the text file’s lines. To do so, we will create a file with the name “File_toRead” with the “.sh” extension:

$ nano File_toRead.sh

Then type the bash script below to read all the lines of the “mybash_file.txt”:

#!/bin/bash
File=”mybash_file.txt”
reads= $(cat $File)
for line in $reads
do
            echo “$line”
done

The explanation of the bash script is:

  • The first line contains the bash code to declare the script as a bash script for making it executable.
  • Then, we store the file in the variable “File”, which is supposed to be read.  
  • Save each file line of the file in the “reads” variable.
  • After this, a “for loop” is utilized to display all the lines of the “reads” variable by storing them in the “line” variable.

After saving the file via the “CTRL+S”, close it via the “CTRL+X”.

In the terminal, run the above script by executing the specified name of the bash:

$ bash File_toRead.sh

The output shows all the lines of the “mybash_file.txt”. 

Example 2: Copy the File to Another

Another usage of the “for loop” is that we can copy the file’s contents to another. To do so, we will make some amendments to the above bash script:

#!/bin/bash
File=”mybash_file.txt”
reads= $(cat $File)
for line in $reads
do
            echo “$line”>>my_newFile.txt
done

The explanation of the bash script is:

  • The first line contains the bash code to declare the script as a bash script for making it executable.
  • Then we store the “mybash_file.txt” file in the variable “File”, which is supposed to be read.  
  • Afterward, save each file line of the file in the “reads” variable.
  • After this, a “for loop” is utilized to display all the lines of the “reads” variable by storing them in the “line” variable.

We are saving each line in each iteration in the “my_newFile.txt”. After this, execute the bash script:

$ bash File_toRead.sh

With the execution of the bash script, the new file has been created, and its contents can be displayed 

using the command:

$ cat my_newFile.txt

The output shows the contents of the “mybash_file.txt” by executing the “my_newFile.txt” file in the terminal. 

Conclusion

In Linux, the “for loop” in bash scripting is utilized to read all the lines of the file and copy them into another file. The “for loop” has the same functionality in Bash scripting as in any other programming language. This post has briefly explained the Bash for each line in a file.