532

I have seen -- used in the compgen command.

For example:

compgen -W "foo bar baz" -- b

What is the meaning of the -- in there?

651

More precisely, a double dash (--) is used in most bash built-in commands and many other commands to signify the end of command options, after which only positional parameters are accepted.

Example use: lets say you want to grep a file for the string -v - normally -v will be considered the option to reverse the matching meaning (only show lines that do not match), but with -- you can grep for string -v like this:

grep -- -v file
| improve this answer | |
  • 3
    Most notably in the Bash set built-in, where it's absolutely necessary. – l0b0 Oct 18 '12 at 9:03
  • 11
    -- works to separate options from regular expressions in grep, but the canonical way is to use -e/--regexp. – l0b0 Oct 18 '12 at 9:05
  • 6
    @l0b0: the pattern to search is normally one of the positional parameters, so it can fit after the --, though you are correct in noting that my example above could also be written as grep -e -v file (although that is very confusing). – Guss Jan 21 '15 at 16:12
  • 4
    Not all bash builtin commands accept -- as the end of option marker. [ and echo don't for instance (one of the reasons echo can't be used reliably). – Stéphane Chazelas Mar 21 '16 at 10:06
49

In man bash we can read in Shell Builtin Commands section:

Unless otherwise noted, each builtin command documented in this section as accepting options preceded by - accepts -- to signify the end of the options.

The :, true, false, and test builtins do not accept options and do not treat -- specially. The exit, logout, break, continue, let, and shift builtins accept and process arguments beginning with - without requiring --. Other builtins that accept arguments but are not specified as accepting options interpret arguments beginning with - as invalid options and require -- to prevent this interpretation.

Note that echo does not interpret -- to mean the end of options.

| improve this answer | |
  • 4
    +1 for referencing man bash (note-to-self for next time : ). – user3773048 Oct 18 '18 at 21:24
47

This marks end of parameter (option) list.

| improve this answer | |
18

POSIX.1-2017

12.2 Utility Syntax Guidelines

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.

http://pubs.opengroup.org/onlinepubs/9699919799/basedefs/V1_chap12.html#tag_12_02

| improve this answer | |

Not the answer you're looking for? Browse other questions tagged or ask your own question.