53GB

How to Read a File Line by Line in Bash



How to Read a File Line by Line in Bash

Certain commands inside Linux allow you to make Bash read a file and extract it, later on displaying it as the output on your screen.

Don’t forget to check out our site http://howtech.tv/ for more free how-to videos!
http://youtube.com/ithowtovids – our feed
http://www.facebook.com/howtechtv – join us on facebook
https://plus.google.com/103440382717658277879 – our group in Google+

In this tutorial we will show you how to write a bash script which will read this text file over here, that is let the bash read it line by line.
This file that is read by bash is then displayed as the output where you previously entered your command.
To learn how to let bash read from a file follow the steps given in the tutorial below.

Step 1 — Specify variables

We will start the while loop and after that we will specify three variables over here as “first”, “second” and “third”. The space between the variables will tell the compiler that the new line has started.
while read FIRST SECOND THIRD

Step 2 — Print variable

After that we will simply print the three variables and for that we will use the echo command and a dollar sign before each variable.
do
echo “————-”
echo “$FIRST”
echo “$SECOND”
echo “$THIRD”

Step 3 — Specify the name

At the end, we will specify the file name from which the data has to be extracted, and through this we will let bash read from a file and provide us with the output of whatever it extracts.
done textfile

Step 4 — Let the bash scrip read the data

And over here, you can see that the bash script is reading the data from the file, line by line and since we have initiated a while loop in the beginning, the loop will continue till it has reached the last line.
And that’s it, by following these steps you can learn and understand how to let bash read a file line by line in Linux.

Exit mobile version