Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bash, read line by line from file, with IFS

Tags:

bash

line

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.

like image 659
wakandan Avatar asked Dec 08 '10 09:12

wakandan


People also ask

How read file line by line in shell script and store each line in a variable?

We use the read command with -r argument to read the contents without escaping the backslash character. We read the content of each line and store that in the variable line and inside the while loop we echo with a formatted -e argument to use special characters like \n and print the contents of the line variable.

How do you read IFS?

You should read that statement in two parts, the first one clears the value of the IFS variable, i.e. is equivalent to the more readable IFS="" , the second one is reading the line variable from stdin, read -r line .


2 Answers

In this case, IFS is set to the empty string to prevent read 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 to read, read won't ever split the input into multiple fields regardless of the value of IFS. It will, however, remove the leading and trailing whitespace as mandated in the POSIX specification (assuming the value of IFS contains whitespace or is unset).

See the POSIX specification for read and field splitting for details about how it works.

like image 156
Richard Hansen Avatar answered Oct 13 '22 00:10

Richard Hansen


In the third example on that page, setting IFS to null prevents word splitting which makes that code not work. Here is that code:

while IFS= read -r field1 field2 field3 ... fieldN
do
    command1 on $field1
    command2 on $field1 and $field3
    ..
    ....
    commandN on $field1 ... $fieldN
done < "/path/to dir/file name with space"

As written, all the words on the line are stored in field1 and field2, etc., are empty. Change the line to this and it will work properly:

while read -r field1 field2 field3 ... fieldN
like image 39
Dennis Williamson Avatar answered Oct 12 '22 23:10

Dennis Williamson