I'm learning bash and I found a tutorial on internet that says these are the same:
while read -r line;
do
...
done < file
$ cat file | while IFS= read -r line;
do
...
done
Are there any subtle differences in these two loops are are they really the same?
cat
command. – Wheelhousecat foo | bar
as opposed tobar <foo
(1) is less inefficient for two reasons: (1a) creation of a pipeline requires an extra fork(); (1b) causingfoo
to first be read bycat
, then written into a pipeline, then read from the pipeline bybar
, is less efficient than simply letting content infoo
be directly read bybar
. (2) in cases wherebar
is a program which has access to theseek()
call (not typically available for bash unless you've written a C extension), giving it a pipeline rather than a file prevents use of this. – BlackmonIFS=
? – Parenteau