为什么到命令组的管道只适用于某些命令?

Why does a pipe to a command group only work for some commands?

我有兴趣将输入的 header 保留在 grep 输出中,就像在问题 Include header in the 'grep' result.

中一样

虽然在问题中提出了一个可行的解决方案(使用 sed),但是 answer with most upvotes attracted my attention. It allows utilizing grep --color, for example. It suggests piping the input to a command group 与命令 headgrep,例如:

ps -ef | { head -1; grep python; }

但是,正如链接的答案和评论中所写,这不适用于管道左侧的所有命令。它适用于 pslsof 但不适用于 dfls 和其他。使用它们时,仅打印组中第一个命令的输出:

$ df | { head -1; grep '/'; }
Filesystem                                   1K-blocks      Used Available Use% Mounted on
$

谁能解释为什么管道到命令组只对某些命令有效?有什么方法可以让它通用吗?

您问题的答案在comments of the linked answer

Apparently ps -e is sending the header line first, then not sending anything, then buffering the rest of the output. head must think the stream is closed after the first line, so it exits, leaving grep to see the rest.

它只是偶然起作用。

Is there any way to make it work universally?

一切皆有可能,但是,您可能需要重新编码和重新编译其他所有内容。所以...不可能,抱歉。