Which is the best way to write a loop that reads from a file?

Viewed 206

I'm learning bash and trying to understand the difference between these two methods of reading lines from a file.

1.

while IFS= read -r line
do
  echo $line
done < "$file"

2.

cat $file |
while read data
do
   echo $i
done

So basically what I'm wondering is: Is either of them more common practice than the other? Are there performance differences? etc.

Also, are there other ways of reading from a file that are even better, especially when it comes to reading from large files?

2 Answers
Related