Introduction
Reading a file line by line allows you to effectively process a file’s contents and output each line as an element in a list. After displaying each line separately, search for or match any specific content easily.
One of the ways to read a text file in individual lines is to use the Bash shell.
In this tutorial, you will learn to read a file line by line in Bash.
Prerequisites
- A system running Linux.
- Access to a terminal (Ctrl + Alt + T).
- A text editor (such as Nano or vi/vim).
Reading Line by Line in Bash
There are several methods for reading a file line by line using Bash. The following sections highlight five methods for processing a file one line at a time using Bash.
Method 1: Using Read Command and While Loop
The first method is to use the read command and a while
loop in a Bash script. While it is possible to do the same in a terminal, Bash scripts save the code and make it reusable. Follow the steps below:
1. Open the terminal (Ctrl + Alt + T) and create a new Bash script using a text editor such as vi/vim:
vi line.sh
2. Enter the following lines:
#!/bin/bash
file="days.txt"
while read -r line; do
echo -e "$line\n"
done <$file
- The
$file
variable is defined after the shebang line (the first line in Bash scripts), and it stores the path to the input file you want to process.
- The
-r
argument appended to theread
command prevents the interpretation of any backslash-escaped characters while reading the file’s contents.
Note: Prevent the read
command from trimming leading/trailing whitespaces by setting the internal field separator to an empty string – IFS=
.
- Each line’s contents are stored in the
$line
variable. Within thewhile
loop, the echo command prints the$line
variable’s contents. The-e
argument allowsecho
to interpret special characters such as the newline character\n
.
- The
while
loop continues until it reaches the end of the file and the loop ends.
Note: Use printf
instead of echo
to print and format the output. Learn to use the Bash printf command.
3. Save the script and exit vi:
:wq
4. Run the script:
bash line.sh
The script outputs each line of the example text file separately.
Method 2: Using cat Command and for Loop
Another method to display a file’s contents in individual lines is to use the cat command and the for
loop. The for
loop allows echo
to print the lines from the cat
command output until it reaches the end of the file.
Note: Learn more about the Bash for loop and see examples of using the loop.
Follow the steps below:
1. Create a new script:
vi readfile.sh
2. Enter the following lines:
#!/bin/bash
file=$(cat days.txt)
for line in $file
do
echo -e "$line\n"
done
- The
$file
variable stores the input file’s contents using thecat
command.
- The
for
loop iterates through each line of thecat
command output and prints it using theecho
command until it reaches the end of the file.
3. Save the script and exit vi:
:wq
4. Run the script:
bash readfile.sh
The script outputs the file’s contents line by line in standard output.
Method 3: Using here Strings
Another method of printing a file’s contents line by line is to use a here
string to feed the file’s contents to the read
command. The here
string connects the contents of a variable, string, or file specified after the <<<
syntax to the standard input of the invoked program.
Note: A here
string is a simpler form of a here document.
Follow the steps below:
1. Create a new Bash script:
vi herestrings.sh
2. Enter the following lines:
#!/bin/bash
while IFS= read -r line; do
printf '%s\n' "$line"
done <<< $(cat days.txt )
- In the
while
loop, theIFS=
argument is an empty string to prevent trimming whitespaces.
- The
-r
argument prevents the interpretation of backslash-escaped characters.
- The
printf
command prints each line of the file. The format specifiers treat the input as a string (%s
) and add a newline character (\n
) after each line.
- The
here
string feeds thecat
command’s output to theread
command.
3. Save the script and exit the editor:
:wq
4. Execute the script:
bash herestrings.sh
The output prints the file’s contents line by line.
Note: Learn how to compare strings using a Bash script.
Method 4: Using File Descriptors
A file descriptor refers to an open file or process. Each process has three default file descriptors:
0
. Standard input.1
. Standard output.2
. Standard error.
Provide the input for the read
command using a file descriptor and output each line from the file’s contents separately. Follow the steps below:
1. Create a new bash script:
vi descriptors.sh
2. Enter the following lines:
#!/bin/bash
while IFS= read -r -u9 line; do
printf '%s\n' "$line"
done 9< days.txt
- In the
while
loop, instruct theread
command to read input from a file descriptor by specifying the-u
argument and the file descriptor number.
Important: When specifying file descriptors, use a number between 4 and 9 to avoid conflict with the internal shell file descriptors.
- The
printf
command treats the input$line
variable as a string (%s
) and adds a newline character (\n
) after printing the$line
contents.
- The
9<
syntax contains the same file descriptor number as in thewhile
loop. The input file’s contents are sent to the specified file descriptor.
3. Save the script:
:wq
4. Execute the script to test the code:
bash descriptors.sh
The script output prints each line of the file separately.
Method 5: Using Process Substitution
Process substitution allows the standard output of a process (or processes) to appear as a file, and feeds it into another process’ standard input. Use process substitution to supply the input file and print each file line separately.
Follow the steps below:
1. Create a Bash script:
vi substitution.sh
2. Enter the following lines:
#!/bin/bash
while IFS= read -r line; do
printf '%s\n' "$line"
done < <(cat days.txt)
- After closing the loop,
cat
the input file enclosed within parentheses<(cat [input_file_path]
to send the process result to theread
command.
Important: Pay attention not to add whitespace characters between the <
character and the left parenthesis (
. Using a whitespace character interprets the code as a redirection and results in an error.
3. Save the script:
:wq
4. Run the script:
bash substitution.sh
Each file line is printed separately in standard output.
Conclusion
This tutorial explained how to read a file’s contents line by line using Bash shell scripts in five different ways. Reading a file line by line is helpful when searching for strings in a file by reading the lines individually.
Next, learn to exit from a loop using Bash break or resume a loop with Bash continue.