The trap is that
IFS=; while read..
sets the IFS
for the whole shell environment outside the loop, whereas
while IFS= read
redefines it only for the read
invocation (except in the Bourne shell).
You can check that doing a loop like
while IFS= read xxx; ... done
then after such loop, echo "blabalbla $IFS ooooooo"
prints
blabalbla
ooooooo
whereas after
IFS=; read xxx; ... done
the IFS
stays redefined: now echo "blabalbla $IFS ooooooo"
prints
blabalbla ooooooo
So if you use the second form, you have to remember to reset : IFS=$' \t\n'
.
The second part of this question has been merged here, so I've removed the related answer from here.
Best Answer
IFS
stands forInput
Internal Field Separator
- it's a character that separates fields. In the example you posted, it is set to new line character (\n
); so after you set it,for
will process text line by line. In that example, you could change the value ofIFS
(to some letter that you have in your input file) and check how text will be split.BTW, from the answer you posted the second solution is that recommended...
As @jasonwryan noticed, it's not
Input
butInternal
. NameInput
came fromawk
in which there is alsoOFS
-Output Field Separator
.