views:

134

answers:

4

In particular, why is that sometimes the options to some commands are preceded by a + sign and sometimes by a - sign?

for example:

sort -f
sort -nr
sort +4n
sort +3nr
+5  A: 

It's completely arbitrary; the command may implement all of the option handling in its own special way or it might call out to some other convenience functions. The getopt() family of functions is pretty popular, so most software written even remotely recently follows the conventions set by those routines. There are always exceptions, of course!

Carl Norum
+3  A: 

These days, the POSIX standard using 'getopt(3)' is widely used as a standard notation, but in the early days, people were experimenting. On some machines, the sort command no longer supports the + notation. However, various commands (notably ar and tar) accept controls without any prefix character - and dd (alluded to by Alok in a comment) uses another convention altogether.

The GNU convention of using '--' for long options was changed from using '+'. Of course, the X11 software uses a single dash before multi-character options. So, the whole thing is a collection of historic relics as people experimented with how best to handle it.

Jonathan Leffler
What is the difference between `man -help` and `man --help`? On my machine, both of them show the same output.
Lazer
`man --help` passes the long option `--help` to `man`. `man -help` passes four options, `-h`, `-e`, `-l`, `-p` to `man`. Probably `man` looks at the first option, `-h`, and prints help and exits. It is up to each command to interpret command line arguments, though.
Alok
@eSKay: as Alok says, it does depend on the command. Many of the X11 commands take long arguments with a single dash; such commands do not allow option grouping. Other commands (those using classic `getopt()`) treat all option letters as single options; if the option take no argument, then another option may follow in a cluster. That is, presumably, what the 'man' command is doing on your machine. Or, it may recognize '--help' as a long option, and '-h' as a short option which it processes immediately, without checking whether the next letter, 'e', is a valid option. It depends on the command.
Jonathan Leffler
See also: http://stackoverflow.com/questions/367309 and http://stackoverflow.com/questions/456797.
Jonathan Leffler
A: 

It's left to apps to parse options hence the inconsistency. Expanding on your sort example these are all equivalent for coreutils:

sort -k3
sort --k 3
sort --key 3
sort --key=3
_POSIX2_VERSION=199209 sort +2
pixelbeat
+1  A: 

A shell command is just a program, and it is free to interpret its command line any way it likes. Unix never had anything like Apple's interface police to make sure that the command-line interface was consistent across applications. As a result, there is inconsistency, especially in older commands.

Peering into my crystal ball, I think command-line tools will slowly migrate toward GNU standards, double dashes and all. (I grew up with single dashes and still find the double dash very awkward, but it is consistent.)

Norman Ramsey