How to Read File Line by Line in Bash Script [3 Methods]

read file line by line in bash script

When writing a bash script, depends on the automation flow sometimes the script has to read the content from the file line by line. Here we learn 3 methods in a bash script to read file line by line.

Method 1: Using Input Redirector

The simplest way to read a file line by line is by using the input redirector in a while loop.

To demonstrate we have created a sample file named 'mycontent.txt' and will use it throughout this tutorial.

$ cat mycontent.txt
This is a sample file
We are going through contents
line by line
to understand

Let's create a script named 'example1.sh' which uses input redirect and loop.

$ more example1.sh
#!/bin/bash
while read y
do
echo "Line contents are : $y "
done < mycontent.txt

How it works:

- Declaring shell which is “bash”
- Starting while loop and saving line contents in variable “y”
- do part of while loop (Starting point)
- echo to print and “$y” means printing variable value i.e. line
- Reading file contents using input redirector “<” & closing with done

Execution of Script & Output:

$ ./example1.sh
Line contents are : This is a sample file
Line contents are : We are going through contents
Line contents are : line by line
Line contents are : to understand

Method 2: Using cat command

The second method is to use cat command and then sending its output as input to the while loop using the pipe.

Create a script file 'example2.sh', with contents as:

$ more example2.sh
#!/bin/bash
cat mycontent.txt | while read y

do
echo "Line contents are : $y "
done

How it works:

- Declaring shell which is “bash”
- Output of cat command sent as input to while loop using pipe
“|” and saving line contents in variable “y”
- do part of while loop (Starting point)
- echo to print and “$y” means printing variable value i.e. line
- Closing of while loop done

Execution of Script & Output:

$ ./example2.sh

Line contents are : This is a sample file
Line contents are : We are going through contents
Line contents are : line by line
Line contents are : to understand

Tip: we can combine all commands and use them as oneliners.

Output:

$ while read line; do echo $line; done < mycontent.txt
This is a sample file
We are going through contents
line by line
to understand

3. Using filename passed as argument

This third method will send filename as an input argument through the command line.

Create a script file named 'example3.sh', as:

$ more example3.sh
#!/bin/bash
while read y
do
echo "Line contents are : $y "
done < $1

How it works:

- Declaring shell which is “bash”
- Starting while loop and saving line contents in variable “y”
- do part of while loop (Starting point)
- echo to print and “$y” means printing variable value i.e. line
- Reading file contents from command line argument i.e. $1 using input redirector “<” & closing with done

Execution of Script & Output:

$ ./example3.sh mycontent.txt 

Line contents are : This is a sample file
Line contents are : We are going through contents Output of Script
Line contents are : line by line
Line contents are : to understand

Handling backslash and escape character

When file content has backslash and escape characters then the read command used to ignore those backslash and escape character.

If we want to read everything from the file then use -r option to prevents backslash escapes from being interpreted and display value into the console.

If file content has file separator like comma or pipe, we can use IFS (Internal Field Separator) to split the file based on delimiter and display the values.

Example Shell Script:

#!/bin/bash
inputfile="foobar.txt"
while IFS=, read -r y
do
echo $y
done < $inputfile

Script flow:

- Assign the file path to 'inputfile' variable
- While loop read the file line by line
- -r prevents backslash escapes from being interpreted
- IFS (Internal Field Separator), if we want to split the line with field separator need to specify the separator value (Ex: if the line has value like 'foo, bar' and want to split the comma then IFS value should be (IFS=,))
- echo will help to print the content to console
- once reached end of file while loop come out

Conclusion

This tutorial explained how to read file content line by line using bash shell script with examples. It helps searching strings in a file by reading lines individually.

Thanks for reading, please leave your suggestions and reviews in the comment section.

Leave a Comment