用于传递Heredoc的多行语法;这是便携式的吗? [英] Multiline syntax for piping a heredoc; is this portable?
问题描述
我熟悉以下语法:
cmd1 << EOF | cmd2
text
EOF
但是刚刚发现bash允许我写:
but just discovered that bash allows me to write:
cmd1 << EOF |
text
EOF
cmd2
(heredoc用作cmd1的输入,而cmd1的输出通过管道传递到cmd2).这似乎是一个非常奇怪的语法.便携吗?
(the heredoc is used as input to cmd1, and the output of cmd1 is piped to cmd2). This seems like a very odd syntax. Is it portable?
推荐答案
是的,POSIX标准允许这样做. 根据2008年版本:
Yes, the POSIX standard allows this. According to the 2008 version:
此处文档应被视为一个单词,该单词以 下一个
<newline>
并继续直到有一行仅包含 分隔符和<newline>
,中间没有<blank>
字符. 然后,如果有的话,下一个此处文档开始.
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"示例:
And includes this example of multiple "here-documents" in the same line:
cat <<eof1; cat <<eof2
Hi,
eof1
Helene.
eof2
因此,进行重定向或管道操作没有问题.您的示例类似于以下内容:
So there is no problem doing redirections or pipes. Your example is similar to something like this:
cat file |
cmd
shell语法(在链接页面的更下方)包括以下定义:
And the shell grammar (further down on the linked page) includes these definitions:
pipe_sequence : command
| pipe_sequence '|' linebreak command
newline_list : NEWLINE
| newline_list NEWLINE
;
linebreak : newline_list
| /* empty */
因此,管道符号可以在行尾跟随,并且仍被视为管道的一部分.
So a pipe symbol can be followed by an end-of-line and still be considered part of a pipeline.
这篇关于用于传递Heredoc的多行语法;这是便携式的吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!