What does the double-dash stand for in a Bash command?

Asked

Viewed 166 times

4

There is a little time that I use Linux and I know that some commands have arguments that use the - at the beginning of a word.

Example:

 ls -la

And some others use two traits, so:

php artisan --help

But I’ve seen some cases where the double dash is used without any word on the front.

Example 1:

 git checkout master -- file.txt

Example 2:

grep -- -v *

Example 3:

npm run lint -- --fix

In the case, the -- is separated from a word, as in the example I gave of the --help. I actually thought that was specific to git, because it was the only command I learned that needed the -- (as in Example 1). However, I noticed that other commands also use --.

So I ask you:

Does the double dash have any special meaning in Bash? Or is it something specific to the above commands?

  • 3

    You have one minute to hear the word from POSIX?

  • I do. All ears

  • 2

    In a nutshell, the -- "loose" is the "end of the flags". From there on, even if something starts with -, is literal value.

2 answers

6


The double dash alone has some special meaning?

Yes, it indicates the end of the command options, making everything that comes after it to be considered operating.

Or is it something specific to the above commands?

Not specific to these commands, but POSIX, which is a standard that these commands obey.

POSIX is an acronym for Portable Operating System Interface, which is a convension (standard) defined by IEEE Std 1003.1-2017. It is here that it is defined that the options are indicated with a single hyphen followed by a letter (can be grouped) and that with two hyphens is an extended name of the option. In this way it is possible to differentiate -Bar, which is the same as passing the three options -B -a -r, of --Bar, which is just an extended name option.

As for the hyphens alone, it is defined:

Guideline 10: The first -- argument that is not an option-argument should be accepted as a delimiter indicating the end of options. Any following Arguments should be treated as operands, Even if they Begin with the '-' Character.

Source

It is worth paying attention to the text "Any following Arguments SHOULD BE treated as operands". That is, if the command in question is in accordance with POSIX, then you must accept the double hyphen as a way to indicate that the rest of the command will only be operands.

So when it’s done git checkout master -- file.txt everything that comes after -- will be operating from the command git checkout, which according to the documentation, has signature git checkout [<tree-ish>] [--] <pathspec>... (Behold git checkout --help for more information). That is, everything that comes after the -- will be value of <pathspec>....

It is also very useful when the value you want to pass is an option of the given command, as happened with the grep, that the value has been passed "-v" as operand. In this case you could imagine a fictitious command that acts as a proxy for other commands: run [<options>] command [<options>...]. It would be necessary to find a way to identify what is the own option run and what will be the option of command. Supposing -V displays the current version of the command, what is the difference of run -V python and run python -V? Whose version would be aired? Already while doing run -- python -V there is no redundancy, since now the -V will be passed as command operand and no longer option.

1

Signify command end options. After double dash/hiffen only positional parameters are accepted.

In your example, grep will search the string "-v" in all files in the directory.

If you do not use "--" grep would interpret -v as command options, which in the case of grep does the inversion of the search, that is, shows what does not match.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.