bash/sh: Reading a file line by line in a shell script

Practice



Need to read a file line by line in a shell script (bash/sh)? Here's a piece of code that will help you:

file="/home/myusername/file.txt"
while read line
do

...
# Do something here with the line variable
...

done < $file

Here, put the name of the file to be read into the file variable, and the script will execute everything written inside the do...done block for each line of the file, with the line itself stored in the line variable.

Comments

To leave a comment

If you have any suggestion, idea, thanks or comment, feel free to write. We really value feedback and are glad to hear your opinion.
To reply

Lectures and tutorial on "LINUX operating system"

Terms: LINUX operating system