POSIX 7
kennytm引用了man bash,但其中大部分也是POSIX 7: http://pubs.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html#tag_18_07_04:
The redirection operators "<<" and "<<-" both allow redirection of lines contained in a shell input file, known as a "here-document", to the input of a command.
The here-document shall be treated as a single word that begins after the next <newline> and continues until there is a line containing only the delimiter and a <newline>, with no <blank> characters in between. Then the next here-document starts, if there is one. The format is as follows:
[n]<<word
here-document
delimiter
where the optional n represents the file descriptor number. If the number is omitted, the here-document refers to standard input (file descriptor 0).
If any character in word is quoted, the delimiter shall be formed by performing quote removal on word, and the here-document lines shall not be expanded. Otherwise, the delimiter shall be the word itself.
If no characters in word are quoted, all lines of the here-document shall be expanded for parameter expansion, command substitution, and arithmetic expansion. In this case, the <backslash> in the input behaves as the <backslash> inside double-quotes (see Double-Quotes). However, the double-quote character ( '"' ) shall not be treated specially within a here-document, except when the double-quote appears within "$()", "``", or "${}".
If the redirection symbol is "<<-", all leading <tab> characters shall be stripped from input lines and the line containing the trailing delimiter. If more than one "<<" or "<<-" operator is specified on a line, the here-document associated with the first operator shall be supplied first by the application and shall be read first by the shell.
When a here-document is read from a terminal device and the shell is interactive, it shall write the contents of the variable PS2, processed as described in Shell Variables, to standard error before reading each line of input until the delimiter has been recognized.
例子
有些例子还没有给出。
引号阻止参数扩展
没有引用:
a=0
cat <<EOF
$a
EOF
输出:
0
报价:
a=0
cat <<'EOF'
$a
EOF
或者(丑陋但有效):
a=0
cat <<E"O"F
$a
EOF
输出:
$a
连字符删除开头制表符
没有连字符:
cat <<EOF
<tab>a
EOF
其中<tab>是一个文字制表符,可以用Ctrl + V <tab>
输出:
<tab>a
用连字符:
cat <<-EOF
<tab>a
<tab>EOF
输出:
a
当然,这是为了让您可以像周围的代码一样缩进您的cat,这更容易阅读和维护。例如:
if true; then
cat <<-EOF
a
EOF
fi
不幸的是,这并不适用于空格字符:POSIX偏爱制表符缩进。呵。