What does Bash operator <<< mean?

2020-01-25 08:12发布

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?

LINE="7.6.5.4"
IFS=. read -a ARRAY <<< "$LINE"
echo "$IFS"
echo "${ARRAY[@]}"

标签: bash shell
3条回答
戒情不戒烟
2楼-- · 2020-01-25 08:17

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:

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

查看更多
我欲成王,谁敢阻挡
3楼-- · 2020-01-25 08:36

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.

查看更多
狗以群分
4楼-- · 2020-01-25 08:37

From man bash

Here Strings A variant of here documents, the format is:

     <<<word

The word is expanded and supplied to the command on its standard input.

The . on the IFS line is equivalent to source in bash.

Update: More from man bash (Thanks 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>".

yet more from 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.

查看更多
登录 后发表回答