我熟悉这个语法:
cat << EOF | cmd text EOF
但只是发现bash允许我写:
cat << EOF | text EOF cmd
(heredoc用作cmd1的输入)。这看起来像一个非常奇怪的语法。它是便携式的吗?
是的,POSIX标准允许这样。
According to the 2008 version:
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.
并且在同一行中包括多个“here-documents”的此示例:
cat <<eof1; cat <<eof2 Hi,eof1 Helene. eof2
所以没有问题做重定向或管道。你的例子类似这样的东西:
cat file | cmd
而shell语法(在链接页面上的更进一步)包括这些定义:
pipe_sequence : command | pipe_sequence '|' linebreak command newline_list : NEWLINE | newline_list NEWLINE ; linebreak : newline_list | /* empty */
因此,管道符号后面可以是行尾,并且仍然被认为是管道的一部分。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。