双破折号 (--) 是何时以及如何作为 Unix/Linux 中选项分隔符的结尾引入的?

53 gnu history posix arguments

我不认为历史Unix 中的 shell/实用程序或像4.4BSD那样“最近”的东西支持使用双破折号(或两个连续的连字符)作为选项分隔符结尾。使用FreeBSD,您可以看到例如在2.2.1 版本(1997)的rm 联机帮助页中引入的注释。但这只是一个命令的文档。

查看我能找到的最古老的GNU fileutils 更改日志,我看到了这个1(略有改动):

Tue Aug 28 18:05:24 1990  David J. MacKenzie  (djm at albert.ai.mit.edu)

* touch.c (main): Don't interpret first non-option arg as a   <---
  time if `--' is given (POSIX-required kludge).  
* touch.c: Add long-named options.
* Many files: Include <getopt.h> instead of "getopt.h" since
  getopt.h will be in the GNU /usr/include.
* install.c: Declare some functions.
* touch.c, getdate.y, posixtime.y, mktime.c: New files, from bin-src.
* posixtime.y: Move year from before time to after it (but
  before the seconds), for 1003.2 draft 10.
Run Code Online (Sandbox Code Playgroud)

这早于Linux。很明显,您可能想要创建一个名称包含与时间规范相同位数(八位或十位十进制数)的名称的文件 - 而不是为现有文件指定时间戳...


  • 那么是posix.1在 Unix shell 中引入了双破折号 ( --) 作为选项分隔符结尾吗?
  • 这一切是否开始是因为有些人想在touch90 年代初的文件名中使用数字,然后这以一种零碎的方式一次一个实用程序持续了十年??
  • 变更日志中的精彩评论是关于什么的?
  • 准则 10参数 - 应被接受为指示选项结束的分隔符。[...])何时引入 POSIX实用程序语法

1. 与相反,即在全局记录所有命令使用中的长选项,这是无关的。在另一方面,你可以看到参考定界符出现在像GNU rm.c于2000年作为一个评论,之前被暴露给最终用户在2005年(该diagnose_leading_hyphen功能)。但这一切都晚了,而且是关于一个非常具体的用例。

wwo*_*ods 39

据我所知,使用--的结束选项-标记开始用shgetopt在系统III UNIX(1980)。

根据Bourne Shell 家族的这段历史,Bourne Shell首次出现在Version 7 Unix (1979) 中。但它没有一种方式set,以从分隔参数选项。所以原始的 Bourne shell 可以:

  • set -e - 开启错误退出模式
  • set arg1 arg2 ...-设置位置参数$1=arg1$2=arg2等等。

但是:set arg1 -e arg2会给你$1=arg1, $2=arg2,并打开 exit-on-error。哎呀。

System III Unix (1980) 修复了该错误并引入了getopt. 根据getopt手册页

NAME
   getopt - parse command options

SYNOPSIS
   set -- `getopt optstring $?`

DESCRIPTION
   Getopt is used to break up options in command lines for easy parsing by
   shell procedures, and to check  for  legal  options.   Optstring  is  a
   string  of  recognized  option letters (see getopt(3C)); if a letter is
   followed by a colon, the option is expected to have an  argument  which
   may or may not be separated from it by white space.  The special option
   -- is used to delimit the end of the options.  Getopt will place --  in
   the  arguments  at  the  end  of  the  options, or recognize it if used
   explicitly.  The shell arguments ($1 $2 . . .) are reset so  that  each
   option  is  preceded  by a - and in its own shell argument; each option
   argument is also in its own shell argument.
Run Code Online (Sandbox Code Playgroud)

据我所知,这是它出现的第一个地方。

从那里,似乎其他命令采用的--约定来解析参数解析歧义(如用例touchrm整个野生你上面举),20世纪80年代的无标天。

其中一些零碎的采用被编入POSIX.1 (1988),这是关于“POSIX-required kludge”的变更日志评论的来源。

但是直到POSIX.2 (1992)才采用了实用程序语法指南,其中包含著名的指南 10:

Guideline 10:    The argument "--" should be accepted as a delimiter
                 indicating the end of options.  Any following
                 arguments should be treated as operands, even if they
                 begin with the '-' character.  The "--" argument
                 should not be used as an option or as an operand.
Run Code Online (Sandbox Code Playgroud)

这就是它从“kludge”到普遍推荐的地方。

  • 如果您不明白为什么解析命令行选项的标准实用程序会有用,也许您还没有尝试过为命令行选项编写 shell 解析器?有点痛啊!如果有其他工具为我做这件事当然会很好……另外,请记住:1980 年没有 Python、没有 Ruby、没有 Java、没有 Perl、没有 PHP——甚至 C++ 还没有被发明,而且“shell 脚本”的整个想法还是很新的。所以标准命令行解析器的想法还是很新颖的! (5认同)