为什么“head -1”不等于“head -n -1”,而是与“head -n 1”相同?

为什么“head -1”不等于“head -n -1”,而是与“head -n 1”相同?

head -numhead -n num与代替head -n -num(其中num是任意数字)相同

例子:

$ echo -e 'a\nb\nc\nd'|head -1
a

$ echo -e 'a\nb\nc\nd'|head -n 1
a

$ echo -e 'a\nb\nc\nd'|head -n -1
a
b
c

head -1似乎没有任何地方记录。

$ head --help

Usage: head [OPTION]... [FILE]...
Print the first 10 lines of each FILE to standard output.
With more than one FILE, precede each with a header giving the file name.

With no FILE, or when FILE is -, read standard input.

Mandatory arguments to long options are mandatory for short options too.
  -c, --bytes=[-]NUM       print the first NUM bytes of each file;
                             with the leading '-', print all but the last
                             NUM bytes of each file
  -n, --lines=[-]NUM       print the first NUM lines instead of the first 10;
                             with the leading '-', print all but the last
                             NUM lines of each file
  -q, --quiet, --silent    never print headers giving file names
  -v, --verbose            always print headers giving file names
  -z, --zero-terminated    line delimiter is NUL, not newline
      --help     display this help and exit
      --version  output version information and exit

NUM may have a multiplier suffix:
b 512, kB 1000, K 1024, MB 1000*1000, M 1024*1024,
GB 1000*1000*1000, G 1024*1024*1024, and so on for T, P, E, Z, Y.

GNU coreutils online help: <https://www.gnu.org/software/coreutils/>
Full documentation at: <https://www.gnu.org/software/coreutils/head>
or available locally via: info '(coreutils) head invocation'

手册页head(在 Fedora 28 上):

HEAD(1)                          User Commands                         HEAD(1)

NAME
       head - output the first part of files

SYNOPSIS
       head [OPTION]... [FILE]...

DESCRIPTION
       Print  the  first  10 lines of each FILE to standard output.  With more
       than one FILE, precede each with a header giving the file name.

       With no FILE, or when FILE is -, read standard input.

       Mandatory arguments to long options are  mandatory  for  short  options
       too.

       -c, --bytes=[-]NUM
              print  the  first  NUM bytes of each file; with the leading '-',
              print all but the last NUM bytes of each file

       -n, --lines=[-]NUM
              print the first NUM lines instead of  the  first  10;  with  the
              leading '-', print all but the last NUM lines of each file

       -q, --quiet, --silent
              never print headers giving file names

       -v, --verbose
              always print headers giving file names

       -z, --zero-terminated
              line delimiter is NUL, not newline

       --help display this help and exit

       --version
              output version information and exit

       NUM may have a multiplier suffix: b 512, kB 1000, K 1024, MB 1000*1000,
       M 1024*1024, GB 1000*1000*1000, G 1024*1024*1024, and so on for  T,  P,
       E, Z, Y.

AUTHOR
       Written by David MacKenzie and Jim Meyering.

REPORTING BUGS
       GNU coreutils online help: <https://www.gnu.org/software/coreutils/>
       Report head translation bugs to <https://translationproject.org/team/>

COPYRIGHT
       Copyright  ©  2017  Free Software Foundation, Inc.  License GPLv3+: GNU
       GPL version 3 or later <https://gnu.org/licenses/gpl.html>.
       This is free software: you are free  to  change  and  redistribute  it.
       There is NO WARRANTY, to the extent permitted by law.

SEE ALSO
       tail(1)

       Full documentation at: <https://www.gnu.org/software/coreutils/head>
       or available locally via: info '(coreutils) head invocation'

GNU coreutils 8.29               December 2017                         HEAD(1)

答案1

信息页面和在线手册对于 GNUhead包含这部分:

为了兼容性,head还支持过时的选项语法 -[NUM][bkm][cqv],只有首先指定它才能被识别。

head -1相同的想法head -n 1是破折号不是减号,而是命令行选项的标记。这是通常的习惯:以破折号开头的内容是控制如何进行处理的选项,命令行中的其他内容是文件名或其他要处理的实际目标。在本例中,它不是单字符选项,而是 的简写-n,但它基本上仍然是一个选项,而不是文件名。但是,在head +1or中head 1+11将被视为文件名。

双破折号----something也具有独特的含义,它本身 ( --) 会停止选项处理,当后面跟有其他内容时,它标志着 GNU 风格的长选项。所以有head --1forhead -n -1不符合习俗。

如果我猜的话,我会假设存在正数的古怪捷径,但负数则不存在,因为前一种情况更常用且更容易实现。 (此外,-n iii标准head仅针对正值线定义。)

相关内容