我熟悉这个语法:
cat << EOF | cmd text EOF
但只是发现bash允许我写:
cat << EOF | text EOF cmd
(heredoc用作cmd1的输入)。这看起来像一个非常奇怪的语法。它是便携式的吗?
是的,POSIX标准允许这样。
According to the 2008 version:
原文链接:https://www.f2er.com/bash/392415.htmlThe 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.
并且在同一行中包括多个“here-documents”的此示例:
cat <<eof1; cat <<eof2 Hi,eof1 Helene. eof2
所以没有问题做重定向或管道。你的例子类似这样的东西:
cat file | cmd
pipe_sequence : command | pipe_sequence '|' linebreak command newline_list : NEWLINE | newline_list NEWLINE ; linebreak : newline_list | /* empty */
因此,管道符号后面可以是行尾,并且仍然被认为是管道的一部分。