【问题标题】:print n lines of a "command's --help" on stdout在标准输出上打印 n 行“命令的 --help”
【发布时间】:2014-08-15 16:36:13
【问题描述】:

这只是我无法解决的好奇心(尝试 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!

【问题讨论】:

    标签: bash shell stdout


    【解决方案1】:

    tree(和其他一些命令)将它们的帮助打印到标准错误,而不是标准输出。您可以使用|& 而不是| 简单地重定向两者:

    tree --help |& head -n2
    

    【讨论】:

    • 或者使用tree --help 2>&1 | head -n2(或类似的)用于4之前的bash版本(我相信这是添加|&的地方)。
    猜你喜欢
    • 2013-12-07
    • 1970-01-01
    • 1970-01-01
    • 2017-10-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-05-01
    • 1970-01-01
    相关资源
    最近更新 更多