Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

bash

loops

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?

like image 257
bork Avatar asked May 14 '17 04:05

bork


2 Answers

The second one is a Useless Use of Cat: http://porkmail.org/era/unix/award.html

I use the done < "$file" form.

No, there is not a better way in Bash. But eliminating one process (cat) is nice.

like image 179
John Zwinck Avatar answered Oct 05 '22 22:10

John Zwinck


There are a few advantages in the first method:

  • it's more straightforward
  • it doesn't use an extra cat and pipe
  • the loop runs in the current shell whereas the second method uses a subshell; variables set or modified inside the loop are visible outside

Even if the while loop were to consume the output of another command (through process substitution as shown below), the first method is better as it eliminates the subshell:

while read -r line; do
  # loop steps
done < <(_command_)

See also:

  • A variable modified inside a while loop is not remembered
  • BashFAQ/001 - How can I read a file (data stream, variable) line-by-line (and/or field-by-field)?
  • BashFAQ/024 - I set variables in a loop that's in a pipeline. Why do they disappear after the loop terminates? Or, why can't I pipe data to read?
  • Looping through the content of a file in Bash
like image 24
codeforester Avatar answered Oct 05 '22 23:10

codeforester