36

Most unixoid commands feature short and long alternatives for command line options, like ls -a and ls --all, or ls -A and ls --almost-all. Why do those two ways exist? One is shorter to type, the other is easier to read and understand. But every time I write a shell script, I need to decide which I want to use. Is it known why the two alternatives exist? Which one was first, why was the other introduced. On DOS/Windows for example there's almost only case-insensitive one-letter options.

3 Answers 3

36

Originally there were only single-character options. Some programs took multiple-character options, but still with a single dash. AFAIK double-dash multiple-character options come from GNU; they were introduced because they are more readable and often more memorable (and you can have more than 52 of them). Many programs now have both: short options for when you're typing on the command line and remember the character, long options for scripts or on the command line if you only remember the longer option name.

5
  • 23
    +1 for mentioning that long options are better for scripts, because they help document things Aug 11, 2010 at 8:59
  • 6
    As for "only 52 of them": Try ls -BartSimpsonIsJustCool (or Great if you're on Solaris. Aug 11, 2010 at 9:31
  • 2
    @AaronDigulla Is this Easter egg?
    – user712092
    Oct 25, 2012 at 16:50
  • 6
    @user712092 No, it's a trick. Under Linux, everything after the I is the argument to the -I option, so it only makes a difference if you have a file called sJustCool in the current directory. The -BartSimpson part can be reordered, except that the S (sort by size) has to stay after the t (sort by date). Also m has no effect when n or o are present. So the command is equivalent to ls -BSainopsr, and if I haven't missed anything you can't remove another letter to achieve the same output and you can reorder these letters freely. Oct 25, 2012 at 18:51
  • 2
    @Gilles: by the way, to be as accurate as possible, there may be 62 different short options (including digits), not 52. Consider gzip -9 filename for instance. Aug 23, 2014 at 13:41
3

Another purpose I can see in having different ways of specifying parameters (short and long) is that when typing, Unix/Linux command-line folks like to have as short as possible commands. However, those can become cryptic, and if a script is written using those short versions, they can become hard to understand down the road. Using the long version can make the SAME command more readable and understandable by someone who is not the guru of this command, especially if this command is actually a locally written script as opposed to a well-known command.

2

You might guess that it is a matter of taste. When typing on the command line a short option might be preferred, especially in the case of run-together-options (e.g. ls -AL). The long options are better at conveying intent, thus you don't need to consult the man page when you read ls --almost-all --dereference.

Naturally, as you gain experience, you might learn that both of the -A and -L short options are sufficiently well known, and don't require this extra documentation. Especially in a complex command that may combine several commands in interesting ways with evaluation, redirection, etc. In such a case you might prefer brevity over documentation.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .