什么是IFS = $'\\ n“的确切含义? [英] What is the exact meaning of IFS=$'\n'?
问题描述
如果下面的例子,其中 IFS
环境变量设置为一个换行符...
If the following example, which sets the IFS
environment variable to a line feed character...
IFS=$'\n'
- 是什么在美元符号的意思
究竟? - 这是什么在这个特定的做
这样吗? - 我在哪里可以阅读更多关于这个具体的用法(谷歌不允许在搜索中的特殊字符,我不知道要寻找什么否则)?
我知道 IFS
环境变量是什么,以及在 \\ n
字符(换行)但为什么不直接使用的格式如下: IFS =\\ n
(不工作)?
I know what the IFS
environment variable is, and what the \n
character is (line feed), but why not just use the following form:
IFS="\n"
(which does not work)?
例如,如果我通过一个文件的每一行要循环,并想用一个for循环,我可以这样做:
For example, if I want to loop through every line of a file and want to use a for loop, I could do this:
for line in (< /path/to/file); do
echo "Line: $line"
done
不过,这并不正确,除非 IFS
工作设置为一个换行符。为了得到它的工作,我不得不这样做:
However, this won't work right unless IFS
is set to a line feed character. To get it to work, I'd have to do this:
OLDIFS=$IFS
IFS=$'\n'
for line in (< /path/to/file); do
echo "Line: $line"
done
IFS=$OLDIFS
注意:我并不需要做同样的事情的另一种方式,我知道很多其他的已经......我只是好奇的 $的'\\ n
并想知道如果任何人都可以给我一个解释就可以了。
Note: I don't need another way for doing the same thing, I know many other already... I'm only curious about that $'\n'
and wondered if anyone could give me an explanation on it.
推荐答案
通常庆典
确实在字符串并非互为preT转义序列。所以,如果你写 \\ n
或\\ n
或的'\\ n'
,那不是断行 - 这是信 N
(在第一种情况)或反斜杠后跟字母 N
(在另外两起案件)。
Normally bash
doesn't interpret escape sequences in string literals. So if you write \n
or "\n"
or '\n'
, that's not a linebreak - it's the letter n
(in the first case) or a backslash followed by the letter n
(in the other two cases).
$'somestring
是字符串转义序列语法。所以不像的'\\ n'
, $的'\\ n'
实际上是一个换行符。
$'somestring'
is a syntax for string literals with escape sequences. So unlike '\n'
, $'\n'
actually is a linebreak.
这篇关于什么是IFS = $'\\ n“的确切含义?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!