# Iterate a Text File in Bash

This is how you iterate thru a text file outputting a line at a time in Bash shell scripting.


IFS='\n'; for i in(cat FILE.TXT); do echo "$i"; done  Author: Binny V A A philosopher programmer who specializes in backend development and stoicism. ## 3 thoughts on “Iterate a Text File in Bash” 1. Foo says: Actually, this is how not to do it — it starts a useless cat process and needs to store the whole file in memory. The right way to do it goes like this: while read line do echo$line
done < FILE.TXT

2. to iterate file lines is a a bad idea. Plus, the expansion aspect mentioned by @mklement0 (even though that probably can be circumvented by bringing in escaped quotes, which again makes things more complex and less readable). Egor Hans Nov 12 ’17 at 14:23

3. If you want the text file line by line including blank lines and terminating lines without CR, you must use a while loop and you must have an alternate test for the final line.