I'm new to working in the shell and the usage of these commands seems arbitrary. Is there a reason one flag has a single dash and another might have a double dash?

up vote 90 down vote accepted

A single hyphen can be followed by multiple single-character flags. A double hyphen prefixes a single, multicharacter option.

Consider this example:

tar -czf

In this example, -czf specifies three single-character flags: c, z, and f.

Now consider another example:

tar --exclude

In this case, --exclude specifies a single, multicharacter option named exclude. The double hyphen disambiguates the command-line argument, ensuring that tar interprets it as exclude rather than a combination of e, x, c, l, u, d, and e.

  • 1
    @kylex, no, since there is no long option named just "c" and the -- means a long option, not a single character option follows. – psusi May 10 '12 at 14:37
  • 10
    Sometimes even long commands can be single-dashed. For example 'cdrecord' uses all single-dashed commands (-eject -dao ...). It all depends on the program, but most(!) of them use - for single and -- for multiple-character (long) commands – mulaz May 10 '12 at 14:42
  • 7
    @mulaz, yes, cdrecord does quite a few goofy things. – psusi May 10 '12 at 14:46
  • 9
    also bear in mind -- used on its own usually signifys the end of options. see here for more info: unix.stackexchange.com/questions/11376/… – Sirex May 10 '12 at 19:51
  • 1
    @killjoy, because whether through ignorance or choice, the authors of those programs did not follow the convention of course. Just like cdrecord mentioned years ago in the above comments. – psusi Apr 2 at 15:25

It all depends on the program. Usually "-" is used for 'short' options (one-letter, -h), and "--" is used for "long"(er) options (--help).

Short options can usually be combined (so "-h -a" is same as "-ha")

In Unix-like systems, the ASCII hyphen–minus is commonly used to specify options. The character is usually followed by one or more letters. An argument that is a single hyphen–minus by itself without any letters usually specifies that a program should handle data coming from the standard input or send data to the standard output. Two hyphen–minus characters ( -- ) are used on some programs to specify "long options" where more descriptive option names are used. This is a common feature of GNU software.

source

  • So why is it java -version and ant -version, then ? – killjoy Mar 30 at 14:48

It's really a convention. However, it can aid parsers to know more efficiently about options passed to the program. Besides, there are neat utilities that can help parsing these commands, such as getopt(3) or the non-standard getopt_long(3) to help parse the arguments of a program.

It is nice, for we can have multiple short options combined, as other answers say, like tar -xzf myfile.tar.gz.

If there was a "lisa" argument for ls, there would probably have a different meaning to type ls -lisa than ls --lisa. The former are the l, i, s, and a parameters, not the word.

In fact, you could write ls -l -i -s -a, meaning exactly the same as ls -lisa, but that would depend on the program.

There are also programs that don't obey this convention. Most notably for my sight, dd and gcc.

short options with single dash vs long options with double dash

short options can be combined into a single argument;

for example: ls -lrt #instead of ls -l -r -t

If we allow long options with single dash, it causes ambiguity. To resolve this we use double dash for long options.

Your Answer

 

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.