Bash 运算符 <<< 是什么意思?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7950268/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What does Bash operator <<< mean?
提问by gsklee
What does the bash operator <<< mean, as inside the following code block? And how come does $IFS remain to be a space, not a period?
bash 运算符 <<< 在以下代码块中是什么意思?为什么 $IFS 仍然是一个空间,而不是一个句点?
LINE="7.6.5.4"
IFS=. read -a ARRAY <<< "$LINE"
echo "$IFS"
echo "${ARRAY[@]}"
采纳答案by Ignacio Vazquez-Abrams
It redirects the string to stdin of the command.
它将字符串重定向到命令的标准输入。
Variables assigned directly before the command in this way only take effect for the command process; the shell remains untouched.
以这种方式直接在命令之前赋值的变量只对命令过程有效;外壳保持不变。
回答by ryanbraganza
From man bash
从 man bash
Here Strings A variant of here documents, the format is:
<<<wordThe word is expanded and supplied to the command on its standard input.
Here Strings here文档的一个变体,格式为:
<<<word这个词被扩展并提供给它标准输入上的命令。
The .on the IFS line is equivalent to sourcein bash.
在.对IFS线相当于source在bash。
Update:More from man bash(Thanks gsklee, sehe)
更新:更多来自man bash(感谢 gsklee,sehe)
IFS The Internal Field Separator that is used for word splitting after expansion and to split lines into words with the read builtin command. The default value is "
<space><tab><new‐line>".
IFS 内部字段分隔符,用于在扩展后进行 分词,并使用 read 内置命令将行拆分为词。默认值为“
<space><tab><new‐line>”。
yet more from man bash
还有更多来自 man bash
The environment for any simple command or function may be augmented temporarily by prefixing it with parameter assignments, as described above in PARAMETERS. These assignment statements affect only the environment seen by that command.
任何简单命令或函数的环境都可以通过在其前面加上参数分配来临时扩充,如上面参数中所述。这些赋值语句仅影响该命令看到的环境。
回答by Barton Chittenden
The reason that IFS is not being set is that bash isn't seeing that as a separate command... you need to put a line feed or a semicolon after the command in order to terminate it:
未设置 IFS 的原因是 bash 没有将其视为单独的命令……您需要在命令后放置换行符或分号才能终止它:
$ cat /tmp/ifs.sh
LINE="7.6.5.4"
IFS='.' read -a ARRAY <<< "$LINE"
echo "$IFS"
echo "${ARRAY[@]}"
$ bash /tmp/ifs.sh
7 6 5 4
but
但
$ cat /tmp/ifs.sh
LINE="7.6.5.4"
IFS='.'; read -a ARRAY <<< "$LINE"
echo "$IFS"
echo "${ARRAY[@]}"
$ bash /tmp/ifs.sh
.
7 6 5 4
I'm not sure why doing it the first way wasn't a syntax error though.
我不确定为什么第一种方法不是语法错误。

