How to Read a File Line By Line in Bash?

While working with the bash scripts, sometimes we have to read all the lines from the bash scripts. By reading every line of the bash scripts, we can extract the outputs or serve a specific programming purpose.

This post will elaborate on how to read a file line by line. The content of the post is as follows:

Let’s start this guide.

Prerequisites: A File and Lines to Read

Before proceeding with the methods, first, create and add some text which will be then read by a bash script. In our case, we used the nano editor to serve the purpose:

$ nano linux_distributions.txt

Next, type some data names into it:

Save the file using “CTRL+S” and exit the editor via the “CTRL+X” command.

Method 1: Using the read Command With the While Loop

In this method, the read command will be utilized with the while loop to read a text file line by line. First, we will create a bash script file named “line_read.sh” with the nano text editor:

$ nano line_read.sh

Then type the below bash script to read the “linux_distributions.txt” file line by line:

#!/bin/bash
file="linux_distributions.txt"
while read -r line; do
echo -e "$line\n"
done <$file

The script’s code is explained below:

  • With the help of the “-r”, read each line of the file in a while loop
  • Save each line in the “line” variable and print it on the screen using the “e” option
  • On completing the while loop, the data has been stored in the “file” variable

Then, use the command mentioned-below to run the “line_read.sh” script:

$ bash line_script.sh

All the lines are being read and displayed on the screen using the bash script.

Method 2: Using the cat Command and for Loop

In this method, we use the cat command with to read all the content of the “linux_distributions.txt” file.

As an experiment, we are using the following code.

#!/bin/bash
file=$(cat linux_distributions.txt)
for line in $file
do
echo -e "$line\n"
done

The explanation of the above script is:

  • Read the file using the cat command and store it in the variable file
  • Use the for loop to display the contents as well as to save them in “file” variable
  • Terminate the for loop with the done word

Run the bash script again of line_read.sh:

$ bash line_read.sh

The contents of the file have been read line by line.

Method 3: Read a File Line by Line in Bash Using File Descriptors

In this method, file descriptors are used in a bash script to read the file line by line, for example, the following lines of code are used in the test bash script.

#!/bin/bash
while IFS= read -r -u9 line; do
printf '%s\n' "$line"
done 9< linux_distributions.txt

In the above bash script,

  • The while loop IFS(internal File Separator) is used to read the lines of the specified file with the -u argument
  • Then display the line with the same descriptor as used in while loop by using the “9<”

Save the file and exit the nano text editor to run the bash script:

$ bash line_read.sh

The bash script is read line by line.

Method 4: Using Different Strings

This is similar to the above method as we used the IFS but will set no arguments unlikely the above method. The bash script’s test code we used is written below:

#!/bin/bash
while IFS= read -r line; do
printf '%s\n' "$line"
done <<< $(cat linux_distributions.txt )

The explanation of the above bash script is:

  • Use the while loop and read all the lines using the IFS
  • Then display the lines by saving them in variable line
  • And in specified the file by using the cat command

Run the above bash script with the command:

$ nano line_read.sh

The lines have been read line by line using the above bash script.

Method 5: Using Process Substitution

In this method, we will substitute the standard output of the file and then treat it as the standard input for the next process. For reading the linux_distributions.txt file, the following script’s code is utilized.

#!/bin/bash
while IFS= read -r line; do
printf '%s\n' "$line"
done < <(cat linux_distributions.txt)

In the above bash script, the only difference as compared to method 4 is that the output file is enclosed in the () and then sent to the process as the input and to see the output, run the command:

$ bash line_read.sh

That’s how you can read a file line by line in Bash.

Conclusion

To read a file line by line in a bash script, we can use the read command with the while loop or the cat command with the for a loop, and the three other methods can also be used to read a file. In this blog, five different methods of reading file line by line in the bash script have been discussed in detail.