Search code examples
linuxbashshellshls

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 ?

Solution

  • 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.