Shell 脚本:"read -a" 处的“<”个数有何不同

Shell Script : What is the difference of the number of "<" at "read -a"

我见过 read -a 有两个 << 和三个 <<< 。有什么区别?

例如:

read -a arr <<"$file"

read -a arr <<<"$file"

man bash 中搜索 <<<<< 以了解它们的工作原理。

您使用 << 创建了 Here Documents:

   Here Documents
       This  type  of  redirection  instructs the shell to read input from the
       current source until a line containing only delimiter (with no trailing
       blanks)  is seen.  All of the lines read up to that point are then used
       as the standard input for a command.

       The format of here-documents is:

              <<[-]word
                      here-document
              delimiter

一个典型的用途是创建一个文件:

cat << EOF > file.txt
hello
there
EOF

您使用 <<< 创建 Here Strings:

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

              <<<word

       The word undergoes brace  expansion,  tilde  expansion,  parameter  and
       variable  expansion,  command  substitution,  arithmetic expansion, and
       quote removal.  Pathname expansion and  word  splitting  are  not  per-
       formed.   The  result  is supplied as a single string to the command on
       its standard input.

典型的用法是为某些命令提供单行,就好像它是 stdin,例如:

uppercase=$(tr [:lower:] [:upper:] <<< hello)

这是笨拙的旧技术的现代等价物:

uppercase=$(echo hello | tr [:lower:] [:upper:])

(更现代的方式是 text=hello; uppercase=${text^^},但这不是重点。)