Process a file line-by-line in bash

From Birnam Designs Wiki

Jump to: navigation, search

Processing a file line-by-line in bash:

    OLDIFS="$IFS"; IFS=$'\n'; for line in $(cat imgs.txt); do echo "line: $line"; done IFS="$OLDIFS"

Setting the IFS to a newline (note the special newline syntax IFS=$'\n') prevents the for loop to iterate over spaces as well, which causes problems if there are any spaces in the lines of the file.

Share This!
This page was last modified on 12 February 2010, at 16:49. This page has been accessed 552 times.