What does Bash operator <<< mean?

2020-01-25 08:34发布

问题:

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[@]}"

回答1:

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.



回答2:

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.



回答3:

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.



标签: bash shell