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
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
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!
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.
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
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.)