Arno Lorentz
Arno Lorentz

Reputation: 733

Difference between the 3 option syntax for commands in bash

In Linux command line, one can use either of two ways to pass options to commands. Either we can use the short option format which uses a single dash followed by a single letter, for example: -o or the long option format which uses two consecutive dashes followed by a word, for example: --option. But recently I came across some commands which in my thinking uses a 'hybrid' of both the formats, which uses a single dash followed by a word, for example: -option. Now I'm not talking about a commands where you can stick multiple short options together like ls -lisa. I'm talking about options where the word after the single dash is just one option and not multiple short options strung together.

I don't seem to understand why there's a third option. Because what I know about the Linux command line is you can have only a short form format or a long form format. Where did the third format came from?

It's actually confusing because sometimes you cannot be sure if the third format is really a dash followed by one option or a dash followed by multiple short options.

Upvotes: 0

Views: 902

Answers (2)

Shachar Shemesh
Shachar Shemesh

Reputation: 8563

I think you are confusing which component does which part of the parsing.

The command line you type into bash gets parsed twice. First it gets parsed by bash. At this stage, spaces are used to separate the different parameters. Quotes and escapes are being taken into consideration. Wildcards are expanded, and $ variables are substituted.

At the end of this phase, we are left with a command line that has a list of strings, the first of which describes the command to be executed. At this point, bash calls execve, and passes it that list of strings.

The next phase of parsing is optional, and is up to each program to carry out. Most programs call getopt_long, a library function that parses options. The one and two dash convention you mention is applied by it (as well as it's older sibling, getopt).

It is, however, up to each program to parse its own parameters. Many programs use getopt_long, which is why you feel, correctly, that it is a standard. Some, however, do not. Those who do not follow their own way.

That's just how things are.

For your programs, you should try to use either getopt_long or some compatible solution, as that causes the least amount of confusion for users.

Upvotes: 2

Ljm Dullaart
Ljm Dullaart

Reputation: 4969

This is not a bash issue. All programs have their on way of handling the options/flags. There are many different styles:

  • the singe letter style with a single hyphen, for example: ls -l

  • the mnemonic-style with double dashes, which seems a preference for GNU-stuff, for example, ls --size

  • the variable=value-style, for example dd if=file of=otherfile

  • options without dashes, as in tar cvzf arghive.tgz

  • You could even use a + instead of a - (as in date +%m).

etcetera.

It is important to understand that bash just passes these options to the programs/commands. So, in the programs you will generally see:

int main(int argc, char *argv[]){

(c-code example). In that case, argv[0] will point to the program-name (to simplify things a bit) and argv[1] will point to the first argument. Depending on the program, that may be different.

A quick scan through the built-in commands reveals that the built-ins always seem to use the minus-single letter (-a) for specifying options.

Upvotes: 3

Related Questions