A fun little bash teaser to which I'd love an explanation.
Two loop constructs I would have though are identical, clearly are not. Seems there's some difference in piping vs redirecting when doing a while
loop.
Input File
Given this sample file called values.txt
with this content:
1
2
3
4
5
6
Piping to while
$ value=0; cat values.txt | while read var; do value=`expr $value + $var`; done
$ echo $value
0
Redirecting to while
$ value=0; while read var; do value=`expr $value + $var`; done < values.txt
$ echo $value
21
To be brief, clearly in the first version each iteration of the while
loop executes effectively as ()
and in the second each iteration iterates as {}
The question is not the difference between ()
and {}
. My question is what causes this difference in behavior for while
loops?
Is there a logical reason they should behave differently or was it just a bad choice made early on that couldn't be changed for compatibility reasons? Is it ever possible to pipe to while
and get {}
behavior?