What are the differences between "-" and "--" in commands?

It is a matter of convention. POSIX standard programs usually only have single character options, and they're all prefixed with a single hyphen. The longer versions are a GNU improvement for clarity, as far as I can tell, and usually are prefixed with double hyphens. You can see this in the libraries and programs used to parse options - getopt and gnu-getopt respectively. Non-GNU and non-POSIX-compliant programs may do something else altogether. Note that you really can't be sure that the long form of an argument may be the same from program to program. -f usually means --force, but not with apt-get install (common misconception). -v may mean version - usually, or --verbose, and so on. Check the man pages or other documentation to be sure. Also have a look at the Wikipedia article on getopt. All this predates Linux by a decade or more.

dd is an example of a POSIX standard utility which doesn't have any hyphenated options at all. find is the classic example of a POSIX standard utility that has word options with single hyphens - nearly all of find's options are multiple characters long. ps supports multiple option styles:

This version of ps accepts several kinds of options:

1   UNIX options, which may be grouped and must be preceded by a dash.
2   BSD options, which may be grouped and must not be used with a dash.
3   GNU long options, which are preceded by two dashes.

GNU tar also supports multiple option styles.

As a matter of usage, prefer the long options in scripts if you can be sure that the scripts will be run in compatible environments - the improvement in clarity is a blessing when debugging. For portability, the short options are preferred.


Usually - options can be chained together, like pacman -Syu being equivalent to pacman -S -y -u, and -- options generally take a parameter as in ./configure --prefix=/usr

Tags:

Command Line