Bash, read line by line from file, with IFS

前端 未结 4 1728
独厮守ぢ
独厮守ぢ 2020-12-09 05:50

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
         


        
4条回答
  •  北海茫月
    2020-12-09 06:21

    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.

提交回复
热议问题