在stdout上打印n行"command's --help"

Yek*_*eke 3 bash shell stdout

这只是我无法解决的好奇心(尝试sed,awk,tail,head等).

这有效:

$ ls --help | head -n 2
Usage: ls [OPTION]... [FILE]...
List information about the FILEs (the current directory by default).
Run Code Online (Sandbox Code Playgroud)

为什么使用其他命令它不起作用?

$ tree --help | head -n 2 
Prints the whole --help! not just the first 2 lines!
Run Code Online (Sandbox Code Playgroud)

Mur*_*nik 6

tree(以及其他一些命令)将他们的帮助打印到stderr,而不是stdout.您可以通过使用|&而不是重定向两者|:

tree --help |& head -n2
Run Code Online (Sandbox Code Playgroud)

  • 或者使用`tree --help 2>&1 | 头部-n2`(或类似的)适用于4岁以上的bash版本(我相信这是添加`|&`的地方). (3认同)