IFS=$'\n' 的确切含义是什么?

发布于 2024-10-01 09:40:06 字数 793 浏览 5 评论 0原文

如果以下示例将 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.

\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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

千紇 2024-10-08 09:40:06

通常 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 letter n (in the first case) or a backslash followed by the letter n (in the other two cases).

$'somestring' is a syntax for string literals with escape sequences. So unlike '\n', $'\n' actually is a linebreak.

触ぅ动初心 2024-10-08 09:40:06

只是为了给该结构赋予其官方名称$'...' 形式的字符串称为ANSI C 引用字符串

也就是说,与 [ANSI] C 字符串一样,可以识别反斜杠转义序列,并扩展为其等效的文字(有关支持的转义序列的完整列表,请参阅下文)。

经过此扩展后,$'...' 字符串的行为方式与 '...' 字符串相同- 即,它们被视为文字,不受任何[进一步] shell 扩展的影响。

例如,$'\n' 扩展为文字换行符 - 这是常规的 bash 字符串文字(无论是 '...' 还是 "。 ..") 做不到。[1]

另一个有趣的功能是 ANSI C 引号字符串可以将 '(单引号)转义为 < code>\',其中,'...'(常规单引号字符串)不能:

echo 

支持的转义序列列表

反斜杠转义序列(如果存在)按如下方式解码:

\a
警报(响铃)

\b
退格键

\e
\E
转义字符(非 ANSI C)

\f
换页符

\n
换行符

\r
回车

\t
水平制表符

\v
垂直制表符

\
反斜杠

\'
单引号

\"
双引号

\nnn
八位字符,其值为八进制值 nnn(一到三位数)

\xHH
值为十六进制值 HH(一位或两位十六进制数字)的八位字符

\uHHHH
Unicode (ISO/IEC 10646) 字符,其值为十六进制值 HHHH(一到四个十六进制数字)

\呃呃呃呃呃呃
Unicode (ISO/IEC 10646) 字符,其值为十六进制值 HHHHHHHH(一到八个十六进制数字)

\cx
一个 control-x 字符

扩展结果是单引号的,就好像美元符号不存在一样。


[1] 但是,您可以在“...”和“...”字符串中嵌入实际换行符;即,您可以定义跨越多行的字符串。

Honey, I\'m home' # OK; this cannot be done with '...'

支持的转义序列列表

反斜杠转义序列(如果存在)按如下方式解码:

\a
警报(响铃)

\b
退格键

\e
\E
转义字符(非 ANSI C)

\f
换页符

\n
换行符

\r
回车

\t
水平制表符

\v
垂直制表符

\
反斜杠

\'
单引号

\"
双引号

\nnn
八位字符,其值为八进制值 nnn(一到三位数)

\xHH
值为十六进制值 HH(一位或两位十六进制数字)的八位字符

\uHHHH
Unicode (ISO/IEC 10646) 字符,其值为十六进制值 HHHH(一到四个十六进制数字)

\呃呃呃呃呃呃
Unicode (ISO/IEC 10646) 字符,其值为十六进制值 HHHHHHHH(一到八个十六进制数字)

\cx
一个 control-x 字符

扩展结果是单引号的,就好像美元符号不存在一样。


[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:

echo 

List of supported escape sequences:

Backslash escape sequences, if present, are decoded as follows:

\a
alert (bell)

\b
backspace

\e
\E
an escape character (not ANSI C)

\f
form feed

\n
newline

\r
carriage return

\t
horizontal tab

\v
vertical tab

\
backslash

\'
single quote

\"
double quote

\nnn
the eight-bit character whose value is the octal value nnn (one to three digits)

\xHH
the eight-bit character whose value is the hexadecimal value HH (one or two hex digits)

\uHHHH
the Unicode (ISO/IEC 10646) character whose value is the hexadecimal value HHHH (one to four hex digits)

\UHHHHHHHH
the Unicode (ISO/IEC 10646) character whose value is the hexadecimal value HHHHHHHH (one to eight hex digits)

\cx
a control-x character

The expanded result is single-quoted, as if the dollar sign had not been present.


[1] You can, however, embed actual newlines in '...' and "..." strings; i.e., you can define strings that span multiple lines.

Honey, I\'m home' # OK; this cannot be done with '...'

List of supported escape sequences:

Backslash escape sequences, if present, are decoded as follows:

\a
alert (bell)

\b
backspace

\e
\E
an escape character (not ANSI C)

\f
form feed

\n
newline

\r
carriage return

\t
horizontal tab

\v
vertical tab

\
backslash

\'
single quote

\"
double quote

\nnn
the eight-bit character whose value is the octal value nnn (one to three digits)

\xHH
the eight-bit character whose value is the hexadecimal value HH (one or two hex digits)

\uHHHH
the Unicode (ISO/IEC 10646) character whose value is the hexadecimal value HHHH (one to four hex digits)

\UHHHHHHHH
the Unicode (ISO/IEC 10646) character whose value is the hexadecimal value HHHHHHHH (one to eight hex digits)

\cx
a control-x character

The expanded result is single-quoted, as if the dollar sign had not been present.


[1] You can, however, embed actual newlines in '...' and "..." strings; i.e., you can define strings that span multiple lines.

蓝颜夕 2024-10-08 09:40:06

来自 http://www.linuxtopia.org/online_books/bash_guide_for_beginners/sect_03_03.html

“$'STRING'”形式的单词是
以特殊方式处理。这个词
扩展为字符串,其中
反斜杠转义字符被替换
由 ANSI-C 标准指定。
反斜杠转义序列可以是
在 Bash 文档中找到。发现

我猜它强制脚本将换行符转义为正确的 ANSI-C 标准。

From http://www.linuxtopia.org/online_books/bash_guide_for_beginners/sect_03_03.html:

Words in the form "$'STRING'" are
treated in a special way. The word
expands to a string, with
backslash-escaped characters replaced
as specified by the ANSI-C standard.
Backslash escape sequences can be
found in the Bash documentation.found

I guess it's forcing the script to escape the line feed to the proper ANSI-C standard.

刘备忘录 2024-10-08 09:40:06

重新恢复默认 IFS - 这个 OLDIFS=$IFS 是不必要的。在子 shell 中运行新的 IFS 以避免覆盖默认的 IFS:

ar=(123 321); ( IFS=

此外,我真的不相信您完全恢复旧的 IFS。您应该将其双引号以避免换行,例如 OLDIFS="$IFS"

\n'; echo ${ar[*]} )

此外,我真的不相信您完全恢复旧的 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:

ar=(123 321); ( 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".

\n'; echo ${ar[*]} )

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".

尐籹人 2024-10-08 09:40:06

ANSI C 引用的字符串是一个关键点。感谢@mklement0。

您可以使用命令 od 测试 ANSI C 引用的字符串。

echo -n 

输出:

0000000  \n  
0000001

0000000   \   n   
0000002

0000000   \   n   
0000002

0000000   \   n   
0000002

通过输出可以清楚地了解其含义。

\n' | od -c echo -n '\n' | od -c echo -n $"\n" | od -c echo -n "\n" | od -c

输出:

通过输出可以清楚地了解其含义。

ANSI C-quoted strings is a key point. Thanks to @mklement0 .

You can test ANSI C-quoted strings with command od.

echo -n 

Outputs:

0000000  \n  
0000001

0000000   \   n   
0000002

0000000   \   n   
0000002

0000000   \   n   
0000002

You can know the meaning clearly by the outputs.

\n' | od -c echo -n '\n' | od -c echo -n $"\n" | od -c echo -n "\n" | od -c

Outputs:

You can know the meaning clearly by the outputs.

原来分手还会想你 2024-10-08 09:40:06

问题:

IFS=$'\n'的确切含义是什么?

简单回答:

嘿巴什!将内部字段分隔符 (IFS) 设置为换行


什么是 IFS

IFS 是 Bash 在处理字符串时用作单词/项目边界的字符。

它设置为空格制表符换行符等空白字符,默认

示例 1:

使用 IFS 的默认值

string="first second:third forth:fifth"

for item in $string; do
    echo "$item"
done

输出:

first
second:third
forth:fifth

示例 2:

IFS 设置为 :

# Set the IFS to collon (:) character
IFS=:

string="first second:third forth:fifth"

for item in $string; do
    echo "$item"
done

输出:

first second  
third forth  
fifth

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

string="first second:third forth:fifth"

for item in $string; do
    echo "$item"
done

Output:

first
second:third
forth:fifth

Example 2:

Set IFS to :

# Set the IFS to collon (:) character
IFS=:

string="first second:third forth:fifth"

for item in $string; do
    echo "$item"
done

Output:

first second  
third forth  
fifth
何其悲哀 2024-10-08 09:40:06

这就像从变量中检索值:

VAR='test'
echo VAR
echo $VAR

是不同的,因此美元符号基本上评估内容。

It's like retrieving the value from a variable:

VAR='test'
echo VAR
echo $VAR

are different, so the dollar sign basically evaluates the content.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文