IFS=$'\n' 的确切含义是什么?
如果以下示例将 IFS
环境变量设置为换行符...
IFS=$'\n'
- 美元符号是什么意思 完全正确?
- 它在这个特定的地方有什么作用 案件?
- 我在哪里可以阅读有关此特定用法的更多信息(Google 不允许在搜索中使用特殊字符,而且我不知道要查找什么)?
我知道 IFS
环境变量是什么,以及 \n
字符是什么(换行),但为什么不直接使用以下形式: IFS="\n"
(这不起作用)?
例如,如果我想循环遍历文件的每一行并想使用 for 循环,我可以这样做:
for line in (< /path/to/file); do
echo "Line: $line"
done
但是,除非将 IFS
设置为换行符,否则这将无法正常工作特点。为了让它工作,我必须这样做:
OLDIFS=$IFS
IFS=$'\n'
for line in (< /path/to/file); do
echo "Line: $line"
done
IFS=$OLDIFS
注意:我不需要另一种方法来做同样的事情,我已经知道很多其他方法了......我只是好奇$'\n'
并想知道是否有人可以给我一个解释。
If the following example, which sets the IFS
environment variable to a line feed character...
IFS=
- What does the dollar sign mean
exactly?
- What does it do in this specific
case?
- Where can I read more on this specific usage (Google doesn't allow special characters in searches and I don't know what to look for otherwise)?
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 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
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=
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.
\n'
- What does the dollar sign mean
exactly?
- What does it do in this specific
case?
- Where can I read more on this specific usage (Google doesn't allow special characters in searches and I don't know what to look for otherwise)?
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 example, if I want to loop through every line of a file and want to use a for loop, I could do this:
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:
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.
\n'
for line in (< /path/to/file); do
echo "Line: $line"
done
IFS=$OLDIFS
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.
- What does the dollar sign mean
exactly? - What does it do in this specific
case? - Where can I read more on this specific usage (Google doesn't allow special characters in searches and I don't know what to look for otherwise)?
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 example, if I want to loop through every line of a file and want to use a for loop, I could do this:
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:
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
通常
bash
不会解释字符串文字中的转义序列。因此,如果您编写\n
或"\n"
或'\n'
,那不是换行符 - 它是字母n
(在第一种情况下)或反斜杠后跟字母n
(在其他两种情况下)。$'somestring'
是带有转义序列的字符串文字的语法。因此,与'\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 lettern
(in the first case) or a backslash followed by the lettern
(in the other two cases).$'somestring'
is a syntax for string literals with escape sequences. So unlike'\n'
,$'\n'
actually is a linebreak.只是为了给该结构赋予其官方名称:
$'...'
形式的字符串称为ANSI C 引用字符串。也就是说,与 [ANSI] C 字符串一样,可以识别反斜杠转义序列,并扩展为其等效的文字(有关支持的转义序列的完整列表,请参阅下文)。
经过此扩展后,
$'...'
字符串的行为方式与'...'
字符串相同- 即,它们被视为文字,不受任何[进一步] shell 扩展的影响。例如,
$'\n'
扩展为文字换行符 - 这是常规的 bash 字符串文字(无论是'...'
还是"。 .."
) 做不到。[1]另一个有趣的功能是 ANSI C 引号字符串可以将
'
(单引号)转义为 < code>\',其中,'...'
(常规单引号字符串)不能:支持的转义序列列表:
[1] 但是,您可以在“...”和“...”字符串中嵌入实际换行符;即,您可以定义跨越多行的字符串。
Just to give the construct its official name: strings of the form
$'...'
are called ANSI C-quoted strings.That is, as in [ANSI] C strings, backlash escape sequences are recognized and expanded to their literal equivalent (see below for the complete list of supported escape sequences).
After this expansion,
$'...'
strings behave the same way as'...'
strings - i.e., they're treated as literals NOT subject to any [further] shell expansions.For instance,
$'\n'
expands to a literal newline character - which is something a regular bash string literal (whether'...'
or"..."
) cannot do.[1]Another interesting feature is that ANSI C-quoted strings can escape
'
(single quotes) as\'
, which,'...'
(regular single-quoted strings) cannot:List of supported escape sequences:
[1] You can, however, embed actual newlines in '...' and "..." strings; i.e., you can define strings that span multiple lines.
来自 http://www.linuxtopia.org/online_books/bash_guide_for_beginners/sect_03_03.html:
我猜它强制脚本将换行符转义为正确的 ANSI-C 标准。
From http://www.linuxtopia.org/online_books/bash_guide_for_beginners/sect_03_03.html:
I guess it's forcing the script to escape the line feed to the proper ANSI-C standard.
重新恢复默认 IFS - 这个
OLDIFS=$IFS
是不必要的。在子 shell 中运行新的 IFS 以避免覆盖默认的 IFS:此外,我真的不相信您完全恢复旧的 IFS。您应该将其双引号以避免换行,例如
OLDIFS="$IFS"
。Re recovering the default IFS- this
OLDIFS=$IFS
is not necessary. Run new IFS in subshell to avoid overriding the default IFS:Besides I don't really believe you recover the old IFS fully. You should double quote it to avoid line breaking such as
OLDIFS="$IFS"
.ANSI C 引用的字符串是一个关键点。感谢@mklement0。
您可以使用命令 od 测试 ANSI C 引用的字符串。
输出:
通过输出可以清楚地了解其含义。
ANSI C-quoted strings is a key point. Thanks to @mklement0 .
You can test ANSI C-quoted strings with command od.
Outputs:
You can know the meaning clearly by the outputs.
问题:
IFS=$'\n'
的确切含义是什么?简单回答:
嘿巴什!将内部字段分隔符 (IFS) 设置为换行
什么是
IFS
?IFS
是 Bash 在处理字符串时用作单词/项目边界的字符。它设置为空格、制表符和换行符等空白字符,默认。
示例 1:
使用
IFS
的默认值输出:
示例 2:
将
IFS
设置为:
输出:
Question:
What is the exact meaning of
IFS=$'\n'
?Simple Answer:
Hey Bash! set the Internal Field Separator (IFS) to New Line
What is
IFS
?IFS
is the character, Bash uses as word/item boundaries when processing character strings.It is set to whitespace characters of space, tab, and newline, by default.
Example 1:
Use default value for
IFS
Output:
Example 2:
Set
IFS
to:
Output:
这就像从变量中检索值:
是不同的,因此美元符号基本上评估内容。
It's like retrieving the value from a variable:
are different, so the dollar sign basically evaluates the content.