Shell脚本:“<”的数量有什么不同?在“读-a”

时间:2015-11-13 20:40:17

标签: bash shell scripting

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

例如:

read -a arr <<"$file"

read -a arr <<<"$file"

1 个答案:

答案 0 :(得分:1)

man bash中,搜索<<<<<,了解其工作原理。

您使用<<创建此处文档

   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^^},但这不是重点。)