什么是IFS =做这个庆典循环:`猫文件|而IFS =读-r线; DO ... done` [英] What does IFS= do in this bash loop: `cat file | while IFS= read -r line; do ... done`
问题描述
我学习bash和我看到这种结构:
I'm learning bash and I saw this construction:
cat file | while IFS= read -r line;
do
...
done
谁能解释一下 IFS =
吗?我知道这是输入字段sepereter,但它被设置成什么?
Can anyone explain what IFS=
do? I know it's input field sepereter, but it's being set to nothing?
推荐答案
IFS
做很多事情,但你问的那个特定的循环。
IFS
does many things but you are asking about that particular loop.
在该循环的作用是的 preserve开头和结尾的空格的在行
。为了说明这一点,先用IFS设置为nothing观察:
The effect in that loop is to preserve leading and trailing white space in line
. To illustrate, first observe with IFS set to nothing:
$ echo " this is a test " | while IFS= read -r line; do echo "=$line=" ; done
= this is a test =
的行
变量包含了所有它在它的标准输入收到的空白。现在,考虑使用默认IFS相同的语句:
The line
variable contains all the white space it received on its stdin. Now, consider the same statement with the default IFS:
$ echo " this is a test " | while read -r line; do echo "=$line=" ; done
=this is a test=
在这个版本中,空白内部的线路仍然是preserved。但是,领先的和尾随空白已被删除。
In this version, the white space internal to the line is still preserved. But, the leading and trailing white space have been removed.
的 -r
选项prevents 读
从治疗反斜杠作为特殊字符。
The -r
option prevents read
from treating backslash as a special character.
要说明这一点,我们使用提供两行到,而
回路内回声命令。观察与 -r
会发生什么:
To illustrate, we use two echo commands that supply two lines to the while
loop. Observe what happens with -r
:
$ { echo 'this \\ line is \' ; echo 'continued'; } | while IFS= read -r line; do echo "=$line=" ; done
=this \\ line is \=
=continued=
现在,没有观察到 -r
会发生什么:
Now, observe what happens without -r
:
$ { echo 'this \\ line is \' ; echo 'continued'; } | while IFS= read line; do echo "=$line=" ; done
=this \ line is continued=
没有 -r
,两个变化发生了。首先,双反斜线被转换为一个反斜杠。其次,在第一行的末尾反斜杠是PTED作为续行字符间$ P $和两行合并成一个。
Without -r
, two changes happened. First, the double-backslash was converted to a single backslash. Second, the backslash on the end of the first line was interpreted as a line-continuation character and the two lines were merged into one.
总之,如果你想在输入反斜杠有特殊的意义,不使用 -r
。如果你想在输入反斜杠被当作普通的字符,然后用 -r
。
In sum, if you want backslashes in the input to have special meaning, don't use -r
. If you want backslashes in the input to be taken as plain characters, then use -r
.
由于读
需要输入一行的时间,IFS行为影响以相同的方式,它影响单行输入多行输入的每一行。 -r
行为异常与同样,如果没有 -r
,多条线路可以使用尾随合并成一条线如上图所示反斜杠。
Since read
takes input one line at a time, IFS behaves affects each line of multiple line input in the same way that it affects single line input. -r
behaves similarly with the exception that, without -r
, multiple lines can be combined into one line using the trailing backslash as shown above.
与多个线路输入的行为,但是,可以大大利用读的 -d
标志改变。 -d
更改分隔符的读
用来标记输入行的末尾。例如,我们可以终止与制表符行:
The behavior with multiple line input, however, can be changed drastically using read's -d
flag. -d
changes the delimiter character that read
uses to mark the end of an input line. For example, we can terminate lines with a tab character:
$ echo $'line one \n line\t two \n line three\t ends here'
line one
line two
line three ends here
$ echo $'line one \n line\t two \n line three\t ends here' | while IFS= read -r -d$'\t' line; do echo "=$line=" ; done
=line one
line=
= two
line three=
在这里, $...
构造用来输入特殊字符,如换行, \\ n
和标签, \\ t
。观察与 -d $'\\ t'
,读
把它投入的基础上制表符的线。最后一个选项卡后,都被忽略。
Here, the $'...'
construct was used to enter special characters like newline, \n
and tab, \t
. Observe that with -d$'\t'
, read
divides its input into "lines" based on tab characters. Anything after the final tab is ignored.
的上述特征的最重要的用途是处理困难的文件名。因为这不能出现在路径/文件名的一个字符是一个空字符,空字符可用于分隔的文件名的列表。作为一个例子:
The most important use of the features described above is to process difficult file names. Since the one character that cannot appear in path/filenames is the null character, the null character can be used to separate a list of file names. As an example:
while IFS= read -r -d $'\0' file
do
# do something to each file
done < <(find ~/music -type f -print0)
这篇关于什么是IFS =做这个庆典循环:`猫文件|而IFS =读-r线; DO ... done`的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!