打印"命令的n行 - 帮助"在stdout上

时间:2014-08-15 16:36:13

标签: 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).

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

$ tree --help | head -n 2 
Prints the whole --help! not just the first 2 lines!

1 个答案:

答案 0 :(得分:6)

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

tree --help |& head -n2