I'm having this code from http://bash.cyberciti.biz/guide/While_loop, used to read line by line from a file
file=/etc/resolv.conf
while IFS= read -r line
do
# echo line is stored in $line
echo $line
done < "$file"
the part I don't understand is IFS=
and how it contributes to this functionality. Could anybody explain this to me? Thanks.
In the third example on that page, setting
IFS
to null prevents word splitting which makes that code not work. Here is that code:As written, all the words on the line are stored in
field1
andfield2
, etc., are empty. Change the line to this and it will work properly:To make
IFS
a genuine line separator, useIFS=$'\012'
.IFS is a variable for the line separator (or actually "Internal Field Separator"). That code will effectively empty out the line separator for your read command and set it to its default. Sometimes IFS is changed somewhere else in the code due to users wanting other "line" endings, like reading one sentence at a time (
IFS=.
) or similar.I guess they included the
IFS=
here just to make sure it works or everyone, regardless of the previous value on the IFS variable. The code should still work withoutIFS=
In this case,
IFS
is set to the empty string to preventread
from stripping leading and trailing whitespace from the line.Changing
IFS
is usually done to control how the input will be split into multiple fields. But in this case, because there's only one variable name given toread
,read
won't ever split the input into multiple fields regardless of the value ofIFS
. It will, however, remove the leading and trailing whitespace as mandated in the POSIX specification (assuming the value ofIFS
contains whitespace or is unset).See the POSIX specification for read and field splitting for details about how it works.