bash中,从文件中读取一行一行,与IFS [英] Bash, read line by line from file, with IFS

查看:360
本文介绍了bash中,从文件中读取一行一行,与IFS的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有从 http://bash.cyberciti.biz/guide/While_loop 这code中,用于从文件中读取一行行

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"

我不明白的部分是 IFS = ,以及它如何有助于实现这一功能。任何人可以解释这样对我?谢谢你。

the part I don't understand is IFS= and how it contributes to this functionality. Could anybody explain this to me? Thanks.

推荐答案

IFS是行分隔符(或实际上是内部字段分隔符)的变量。这code将有效地空出的行分隔符为你读命令,并将其设置为默认。有时,IFS是其他地方改变了code由于用户希望其他线的结局,就像在一个时间( IFS = )或类似的阅读一句话。

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.

我猜他们包括 IFS = 这里只是为了确保它的工作原理或每个人,无论在IFS变量previous价值。在code应该还是没有 IFS =

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 without IFS=

这篇关于bash中,从文件中读取一行一行,与IFS的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆