Read lines in a text file with bash

In bash it’s very easy to read a text file line by line and do something to each line. I do this in cygwin a lot to parse logs and other large files.

Your input file needs to have unix-style line endings or bash will get very confused. This trips me up a lot when working on files created with a Windows program.

There are many other ways of doing for/next loops in bash, but this is simple, fast and easy for quick scripts.

#! /bin/bash
while read line
echo $line
done < your_file.txt

If you want/need to use a batch file to read lines look at Batch file to read a text file and perform actions

Leave a Reply