简体   繁体   中英

BASH : Difference between '-' and '--' options

I was working my way through a primer on Shell (Bash) Scripting and had the following doubt :

  • I came across the ls command
  • The man page of ls lists a few use cases as :

    ls -a
    ls --block-size='M'

My Question :

  • What is the difference in - and -- ?
  • Why are there 2 nomenclatures used ?
  • What is the motivation behind it ?

Long-form ( --foo ) options are a GNU extension -- something present in GNU ls , but not present at all in the POSIX standard setting requirements for UNIX tools, so other versions of ls are not obliged to support these options. The entire word ( foo ) is meaningful in this case. This nomenclature was added more recently, and is more expressive than the short form (and doesn't have namespace limitations).

Short-form options ( -al ) are, at least in form, standardized (though extensions can add new ones). They're handled character by character, one letter at a time -- so -al means -a (show hidden files) and -l (long output), rather than having -al have its own meaning in this case. This is the original syntax for UNIX command-line options, and is thus supported not only for terseness but also for backwards compatibility.

They both achieve the same task: passing parameters to the program being called. There aren't many ways of doing that nor a standard way: those are the two main ones known as short option and long option (GNU style).
A program does not need to implement them both, although the way it's usually done lets handling them as unique.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM