×
bash read file line by line

In some cases, you need to read a file line by line and there are several ways to read a file in a bash script. The while loop is the best option to read a file line by line in Linux and in this article, we will show you read a file line by line in bash script with several examples that prints each line.

Basic Syntax

The basic syntax to read a file line by line as shown below:

while IFS= read -r line
do
echo "$line"
done < inputfile

Where :

  • -r : This option is used to prevents backslash escapes from being interpreted.
  • IFS : This option is used to prevent leading/trailing white-space from being trimmed.
  • inputfile : This is the name of the file you want to read line by line.

Create a Sample File for Testing

For testing purpose, create a sample file named file.txt as shown below:

nano file.txt

Add the following contents:

India
Bangladesh
Pakistan
Australia
England
Srilanka

Save and close the file when you are finished.

Read File from Command Line

You can easily read a file from the command line without using the cat command.

Let’s read the file file.txt line by line using while loop as shown below:

while IFS= read -r line; do echo $line; done < file.txt

This command will read each line from the file file.txt and store the content of the line in $line variable then printed it later.

India
Bangladesh
Pakistan
Australia
England
Srilanka

Read File Using Bash Script

You can also create a bash script and read any file line by line.

Let’s create a readfile.sh script.

In this example, n variable is used to keep the value of the line number of the file and while loop is used to read this file with line number.

nano readfile.sh

Add the following contents:

#!/bin/bash
n=1
while IFS= read -r line; do
# reading each line
echo "Line No. $n : $line"
n=$((n+1))
done < /root/file.txt

Save and close the file when you are finished.

Then, run the script as shown below:

bash readfile.sh

You should see the following output:

Line No. 1 : India
Line No. 2 : Bangladesh
Line No. 3 : Pakistan
Line No. 4 : Australia
Line No. 5 : England
Line No. 6 : Srilanka

Passing Filename as an Argument and Reading the File

You can also take a filename as an argument and read the file line by line.

Let’s create a readfile.sh script.

nano readfile.sh

Add the following contents:

#!/bin/bash
while IFS= read -r line; do
echo "$line is the country name"
done < $1

Save and close the file when you are finished.

Then, run the script as shown below:

bash readfile.sh

You should see the following output:

India is the country name
Bangladesh is the country name
Pakistan is the country name
Australia is the country name
England is the country name
Srilanka is the country name

Read a File Using a Process Substitution

Process Substitution allows the input or output of a command to appear as a file.

Let’s take a look at the following example.

nano readfile.sh

Add the following contents:

#!/bin/bash
while IFS= read -r line
do
echo "Welcome to $line"
done < <(cat /root/file.txt )

Save and close the file when you are finished. Then, run the script as shown below:

bash readfile.sh

You should see the following output:

Welcome to India
Welcome to Bangladesh
Welcome to Pakistan
Welcome to Australia
Welcome to England
Welcome to Srilanka

Read a File Using a Here String

Here String allows you to pass multiple lines of input to a command.

Let’s see the following example:

nano readline.sh

Add the following contents:

#!/bin/bash
while IFS= read -r line
do
echo "$line"
done <<< $(cat /root/file.txt )

Save and close the file when you are finished. Then, run the script as shown below:

bash readfile.sh

You should see the following output:

India Bangladesh Pakistan Australia England Srilanka

Conclusion

In this tutorial, we’ve learned how to read a file line by line in a bash script with several examples.

Feel free to ask questions below in the comments if you have any!