Skip to main content
The -- (double dash) token is a POSIX-standard end-of-options indicator (or delimiter). When a command parser encounters --, it ceases option processing and treats all subsequent arguments strictly as literal operands, regardless of whether those arguments begin with a hyphen (-).

Parsing Mechanics

In Bash, argument parsing is typically handled by the getopts builtin for shell scripts, or by external utilities utilizing C library functions like getopt() or getopt_long(). These parsers recognize -- as a hardcoded termination token defined by POSIX Utility Syntax Guideline 10. When the parser reads the argument vector (argv):
  1. It evaluates arguments starting with - as flags or options.
  2. Upon encountering the exact string --, the parser halts its scanning loop.
  3. The parser increments its internal index (such as OPTIND in Bash) past the -- token. It does not automatically consume or discard the -- token from the shell’s positional parameters.
  4. All remaining elements in the argument vector are treated as non-option operands. For external commands, these are processed internally via their C argv array. For Bash builtins or shell scripts, they are processed as literal strings or assigned to the shell’s positional parameters (e.g., $1, $2), depending on the specific command.

Interaction with set

While -- is a universal POSIX guideline implemented by most standard utilities (e.g., rm, grep, ls), it has a specific, notable behavior when used with the Bash set builtin. The set command uses -- to safely assign values to the shell’s positional parameters without them being interpreted as shell options.

Interaction with getopts

When writing custom Bash scripts, the -- delimiter directly affects the OPTIND (Option Index) variable used by the getopts builtin.
If this script is invoked as ./script.sh -x foo -- -y bar:
  1. getopts parses -x and its argument foo.
  2. getopts encounters --, stops parsing, and increments OPTIND past the -- token.
  3. The shift command removes -x, foo, and -- from the parameter list.
  4. The string -y is never evaluated as an option; it is retained as the literal string $1, and bar becomes $2.
Tired of Poor Bash Skills? Fix That With Deep Grasping!Learn More