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

7 linux head

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

\n\n

例子:

\n\n
$ echo -e 'a\\nb\\nc\\nd'|head -1\na\n\n$ echo -e 'a\\nb\\nc\\nd'|head -n 1\na\n\n$ echo -e 'a\\nb\\nc\\nd'|head -n -1\na\nb\nc\n
Run Code Online (Sandbox Code Playgroud)\n\n

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

\n\n

$ head --help

\n\n
Usage: head [OPTION]... [FILE]...\nPrint the first 10 lines of each FILE to standard output.\nWith more than one FILE, precede each with a header giving the file name.\n\nWith no FILE, or when FILE is -, read standard input.\n\nMandatory arguments to long options are mandatory for short options too.\n  -c, --bytes=[-]NUM       print the first NUM bytes of each file;\n                             with the leading '-', print all but the last\n                             NUM bytes of each file\n  -n, --lines=[-]NUM       print the first NUM lines instead of the first 10;\n                             with the leading '-', print all but the last\n                             NUM lines of each file\n  -q, --quiet, --silent    never print headers giving file names\n  -v, --verbose            always print headers giving file names\n  -z, --zero-terminated    line delimiter is NUL, not newline\n      --help     display this help and exit\n      --version  output version information and exit\n\nNUM may have a multiplier suffix:\nb 512, kB 1000, K 1024, MB 1000*1000, M 1024*1024,\nGB 1000*1000*1000, G 1024*1024*1024, and so on for T, P, E, Z, Y.\n\nGNU coreutils online help: <https://www.gnu.org/software/coreutils/>\nFull documentation at: <https://www.gnu.org/software/coreutils/head>\nor available locally via: info '(coreutils) head invocation'\n
Run Code Online (Sandbox Code Playgroud)\n\n

手册页head(在 Fedora 28 上):

\n\n
HEAD(1)                          User Commands                         HEAD(1)\n\nNAME\n       head - output the first part of files\n\nSYNOPSIS\n       head [OPTION]... [FILE]...\n\nDESCRIPTION\n       Print  the  first  10 lines of each FILE to standard output.  With more\n       than one FILE, precede each with a header giving the file name.\n\n       With no FILE, or when FILE is -, read standard input.\n\n       Mandatory arguments to long options are  mandatory  for  short  options\n       too.\n\n       -c, --bytes=[-]NUM\n              print  the  first  NUM bytes of each file; with the leading '-',\n              print all but the last NUM bytes of each file\n\n       -n, --lines=[-]NUM\n              print the first NUM lines instead of  the  first  10;  with  the\n              leading '-', print all but the last NUM lines of each file\n\n       -q, --quiet, --silent\n              never print headers giving file names\n\n       -v, --verbose\n              always print headers giving file names\n\n       -z, --zero-terminated\n              line delimiter is NUL, not newline\n\n       --help display this help and exit\n\n       --version\n              output version information and exit\n\n       NUM may have a multiplier suffix: b 512, kB 1000, K 1024, MB 1000*1000,\n       M 1024*1024, GB 1000*1000*1000, G 1024*1024*1024, and so on for  T,  P,\n       E, Z, Y.\n\nAUTHOR\n       Written by David MacKenzie and Jim Meyering.\n\nREPORTING BUGS\n       GNU coreutils online help: <https://www.gnu.org/software/coreutils/>\n       Report head translation bugs to <https://translationproject.org/team/>\n\nCOPYRIGHT\n       Copyright  \xc2\xa9  2017  Free Software Foundation, Inc.  License GPLv3+: GNU\n       GPL version 3 or later <https://gnu.org/licenses/gpl.html>.\n       This is free software: you are free  to  change  and  redistribute  it.\n       There is NO WARRANTY, to the extent permitted by law.\n\nSEE ALSO\n       tail(1)\n\n       Full documentation at: <https://www.gnu.org/software/coreutils/head>\n       or available locally via: info '(coreutils) head invocation'\n\nGNU coreutils 8.29               December 2017                         HEAD(1)\n
Run Code Online (Sandbox Code Playgroud)\n

ilk*_*chu 7

GNU 的信息页面和在线手册head包含这部分:

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

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

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

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