Skip to main content

Documentation Index

Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt

Use this file to discover all available pages before exploring further.

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 (-).
command [options] -- [operands]

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.

# The shell interprets '-x' as an option, enabling xtrace.
set -x 


# The shell stops option processing at '--'. 

# '-x' is assigned to the positional parameter $1.
set -- -x 


# Passing '--' with no subsequent arguments unsets all positional parameters.
set --

Interaction with getopts

When writing custom Bash scripts, the -- delimiter directly affects the OPTIND (Option Index) variable used by the getopts builtin.
#!/bin/bash


# Standard option parsing loop.

# The leading colon in ":x:y" enables silent error reporting.

# This suppresses default shell errors, allows $OPTARG to capture the invalid 

# option character for the \? case, and redirects missing required arguments 

# to the : case (instead of \?).
while getopts ":x:y" opt; do
  case ${opt} in
    x ) echo "Option x: $OPTARG" ;;
    y ) echo "Option y triggered" ;;
    : ) echo "Missing argument for option: -$OPTARG" >&2; exit 1 ;;
    \? ) echo "Invalid option: -$OPTARG" >&2; exit 1 ;;
  esac
done


# Shift the parsed options and the '--' token out of the positional parameters
shift $((OPTIND - 1))


# Use "$*" to safely join positional parameters into a single string

# without unintended word splitting or pathname expansion.
echo "Remaining operands: $*"
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.
Master Bash with Deep Grasping Methodology!Learn More