Home » Scripting and Automation » Bash / Shell Scripts » How to read a file Line by Line in Bash Script ?

How to read a file Line by Line in Bash Script ?

When you want to process some large data from a text file with one line at a time, there is no better solution than to write a bash script in Linux and give the text file as input to this script and add computational logic to take required actions on line read from file inside bash script.

The below code, assumes you have one file called “mydata.txt” in your current directory from where you are going to execute the script.

#!/bin/bash

while IFS= read -r line_from_text_file

do

        echo $line_from_text_file

done < mydata.txt

You can also, pass this text file name as input to bash script from command line and run the script as,

#!/bin/bash

FILENAME=$1

while IFS= read -r line_from_text_file

do

        echo $line_from_text_file

done < $FILENAME

For two different files, mydata.txt and readme.txt, we can run the same script without any modifications as,

$ bash readline.sh mydata.txt

and

$ bash readline.sh readme.txt

Subscribe our Rurban Life YouTube Channel.. "Rural Life, Urban LifeStyle"

Leave a Comment