搜索
linux中间有的命令是一个横杠,有的命令是两个横杠。请问这有什么区别吗?
巴扎黑
巴扎黑 2017-04-17 12:01:16
[Linux讨论组]

比如 mkdir --help 和 mkdir -p

是不是使用简化的命令就是一个横岗,使用全部命令就是两个?
比如 ls -a 与 ls --all......

请问linux大神是这样吗?

巴扎黑
巴扎黑

全部回复(3)
伊谢尔伦

-a 这种叫做「short-form」
--all 这种叫做「long-form」

在解析的时候是有所区别的

以下来自man getopt:

The getopt utility 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;

伊谢尔伦

正好前段时间看《UNIX 编程艺术》中 10.5 节提到,过来回答一下。

Unix 程序的命令行选项有三种风格:Unix 风格、GNU 风格和 X toolkit 风格。

Unix 风格

即题主说的 -a 这种。在选项需要加参数的时候,紧跟在选项后面即可(或者加空格)。比如登录 mysql server 的时候:

$ mysql -u root -p

或者

$ mysql -uroot -p

均可。这时,root 就是 u 的参数,表示使用 root 用户登录。另外加不加空格看程序怎么才处理了,没有明确的规定。

GNU 风格

即题主说的 --help 这种,使用两个连字符加上关键词(而不是单个字符)。这种风格的出现是因为有一些复杂的 GNU 程序,仅仅 26 个字母(或者算上大小写 52 个)不够使用而发展出来的。另外一个有点是容易理解,因为出现的不再是缩写的字母。选项参数可以使用空格分割也可以使用"="来分割。如:

$ ls --human-readable --sort=time

如果使用 Unix 风格,那么上条命令则是

$ ls -ht

是不是更加易读呢?

X toolkit 风格

这是一种比较不常见的风格,使用单个连字符加上关键词。只有 X 相关的程序才使用这种风格,一般不建议使用。

$ xeyes -display joesws:0 -geometry 1000x1000+0+0

看上去和 GNU 风格差不多,只是双连字符改成了单个连字符。

PHP中文网

In Unix-like systems, the ASCII hyphen-minus is commonly used to specify options. The character is usually followed by one or more letters. Two hyphen-minus characters (--) often indicate that the remaining arguments should not be treated as options, which is useful for example if a file name itself begins with a hyphen, or if further arguments are meant for an inner command. Double hyphen-minuses are also sometimes used to prefix "long options" where more descriptive option names are used. This is a common feature of GNU software. The getopt function and program, and the getopts command are usually used for parsing command-line options.

http://en.wikipedia.org/wiki/Command-line_interface#Arguments

热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号