BASH Programming

Bash: while read line

When you are working on bash scripts, sometimes you may need to read a file line by line. Let’s explain with an example. You have some data in a text file that should be executed or processed by using a script. So, running a bash script to process a text file is much different. You need to follow a specified syntax to read a file line by line. This article will help you to read a line from a file using the while loop in Bash.

Basic Syntax of while read line

The following syntax is used for bash shell to read a file using while loop:

while read -r line;
do
   echo "$line" ;
done < input.file

The option ‘-r’ in the above-mentioned syntax passed to read command that avoids the backslash escapes from being interpreted. The ‘input_file’ option has represented the name of your file that you want to access by using the ‘read’ command.

The internal field separator abbreviated as IFS can be used before the read command is set to the null string that prevents leading or trailing whitespace from being trimmed.

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

Open the terminal using ‘Ctrl + Alt + t’ shortcut and then run the following commands on it.

Example # 1: File Reading line by line

Let’s take an example in which suppose we have a file named ‘OS.txt’ containing the names of all important Linux distributions. If you want to read a file without using the ‘cat’ command then, for this purpose you can execute the following command to perform the particular task. We will use the while loop that will read each line from the file ‘OS.txt’ and stores the content in each step in a variable $line that you can display later.

Paste the following names of Linux distributions in the ‘OS.txt’

CentOS
Ubuntu
Debian
LinuxMint
while read line;
do
echo $line;
done < OS.txt


From the above command, you will get the following response on the terminal window:

Example # 2: Reading file using the bash script

Create a bash file and then add the below-mentioned code in this file to read the file content. You can store the previous text file into a new variable $filename and variable $n is used to keep the value of each line. Now, using the while loop we will read each line from a file with a particular line number.

#!/bin/bash
filename='OS.txt'
n=1
while read line;
do
# for read each line
echo "OS distribution line no. $n : $line"
n=$((n+1))
done < $filename

Save the file with a name ‘OSinfo.sh’ and type the following command on the terminal to run the above bash script.

$ bash OSinfo.sh

Now, run the cat command to view the original file content.

$ cat OS.txt

Alternative Method for file reading

Using passing file name from a command

In a bash file, you need to add the following code script. In this script, we have to take a filename as an argument. First, the value of an argument is read by a $1 variable which has a filename for reading.  It will check that filename exists at the specified location then using the while loop it read a file line by line similar to the previous example.

#!/bin/bash
filename=$1
if [[ ! -f $filename ]] ; then
    echo "Error, you must enter a filename as a command line parameter"
    exit
fi
while read line; do
    # reading each line
    echo $line
done < $filename

Save the above script with named ‘Readline.sh’ and execute the following command on the terminal to run the above-mentioned script:

$ bash Readline.sh OS.txt

You can see the parameter was used as $filename, and read line by line with the while read line syntax from the parameter supplied on the command line.

Conclusion

In this article, we have discussed how to read lines using the while loop in bash programming. We have implemented different methods using the bash script or you can simply use a text file to perform reading a file line by line task. If you are interested to learn more examples then using the above-mentioned syntax you can execute on your system as well. I hope you enjoyed this tutorial and would unique for you. Let’s know in case of any error.

About the author

Karim Buzdar

Karim Buzdar holds a degree in telecommunication engineering and holds several sysadmin certifications. As an IT engineer and technical author, he writes for various web sites. He blogs at LinuxWays.