Bash: Read File Line By Line – While Read Line Loop

The while loop is the best way to read a file line by line in Linux.

If you need to read a file line by line and perform some action with each line – then you should use a while read line construction in Bash, as this is the most proper way to do the necessary.

In this article i will show the general syntax of the while read line construction in Bash and an example of how to read a file line by line from the Linux command line.

I will also show an example of the Bash script that reads an input file line by line and prints each line with some appended text.

Cool Tip: Make your Bash script interactive! Teach it to prompt for “Yes/No” confirmation. Read more →

While Read Line Loop in Bash

The general while read line construction that can be used in Bash scripts:

while read LINE
    do COMMAND
done < FILE

The same construction in one line (easy to use on the Linux command line):

while read LINE; do COMMAND; done < FILE

As example lets print all users from the /etc/passwd file:

$ while read LINE; do echo "$LINE" | cut -f1 -d":"; done < /etc/passwd
root
daemon
bin
[...]

 

The LINE in this construction – is the name of a variable that stores the line during each loop iteration.

 

You can change it to the more appropriate name, depending on the contents of a FILE.

For example, if you store a list of users in a FILE – it would be better to name this variable not the LINE but the USER, as follows:

$ cat users.txt
Eric
Kyle
Stan
Kenny
$ while read USER; do echo "Hello $USER!"; done < users.txt
Hello Eric!
Hello Kyle!
Hello Stan!
Hello Kenny!

Cool Tip: Write a command once and have it executed N times using Bash FOR loop! Read more →

Bash Script: Read File Line By Line

 

Lets create a Bash script, that takes a path to a file as an argument and prints "This is a line:" before the each line of this file.
Create an empty readfile.sh file with the touch readfile.sh command.

 

Make it executable with chmod +x readfile.sh.

Open the readfile.sh with a text editor and put the following code:

#!/bin/bash
FILE=$1
while read LINE; do
     echo "This is a line: $LINE"
done < $FILE

Cool Tip: Do not be a bore! Add COLORS to your Bash script! Make it look AWESOME! Read more →

Save and execute the script:

$ ./script.sh /etc/passwd
This is a line: root:x:0:0:root:/root:/bin/bash
This is a line: bin:x:1:1:bin:/bin:/sbin/nologin
This is a line: daemon:x:2:2:daemon:/sbin:/sbin/nologin
[...]
0 (0)
Article Rating (No Votes)
Rate this article
Attachments
There are no attachments for this article.
Comments
There are no comments for this article. Be the first to post a comment.
Full Name
Email Address
Security Code Security Code
Related Articles RSS Feed
O’Reilly’s CD bookshelf
Viewed 12346 times since Wed, Jun 27, 2018
10 Xargs Command Examples in Linux / UNIX
Viewed 3803 times since Fri, Jun 1, 2018
HowTo: Clear BASH History
Viewed 2068 times since Mon, Feb 18, 2019
Linux: Repeat Command N Times – Bash FOR Loop
Viewed 2904 times since Mon, Feb 18, 2019
perl podstawy
Viewed 1980 times since Mon, May 21, 2018
View Config Files Without Comments
Viewed 2142 times since Mon, May 21, 2018
Using Shell Redirection: All About the Here-Doc
Viewed 10248 times since Wed, May 30, 2018
Linux / UNIX: Convert Epoch Seconds To the Current Time
Viewed 2971 times since Fri, May 25, 2018
Linux and Unix xargs command tutorial with examples
Viewed 3795 times since Fri, Jun 1, 2018
30 Examples for Awk Command in Text Processing
Viewed 14685 times since Sat, Nov 24, 2018