Basic Syntax of while read line
The following syntax is used for bash shell to read a file using while loop:
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.
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’
Ubuntu
Debian
LinuxMint
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.
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.
Now, run the cat command to view the original file content.
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.
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:
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.