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) operator is a POSIX-compliant command-line construct that signifies the end of command options. It instructs the argument parser to cease evaluating subsequent tokens as flags or options, forcing all remaining arguments to be treated strictly as literal positional operands, even if they begin with a hyphen (-).

Syntax

command [options] -- [operands]

Parsing Mechanics and State Management

When a script processes arguments using standard parsers, the -- token is not automatically consumed or deleted from the positional parameters array ($@). It remains a literal string in the array until explicitly handled by the script.
  • getopts (Bash Built-in): When getopts encounters --, it halts option parsing, returns a non-zero exit status to terminate the parsing loop, and updates the OPTIND variable to the index of the first operand following the --. The -- token remains in $@. The developer must manually discard the parsed options and the -- token by executing shift $((OPTIND - 1)).
  • getopt (External Utility): The getopt command normalizes arguments and explicitly preserves (or inserts) the -- token in its output. This ensures the script’s subsequent case statement can detect the -- token, terminate the evaluation loop, and execute a shift command to move past it.

Token Resolution Example

Because parsers do not automatically consume the operator, the positional parameters retain the -- token until a shift operation occurs.

# Invocation
./script.sh -x -y -- -z --foo


# Initial Positional Parameters ($@) Before Shifting

# $1 -> "-x"    (Parsed as Option)

# $2 -> "-y"    (Parsed as Option)

# $3 -> "--"    (Literal string, explicitly present until shifted)

# $4 -> "-z"    (Treated as Positional Operand 1)

# $5 -> "--foo" (Treated as Positional Operand 2)

Variable Expansion Protection

The most critical application of the -- operator in Bash scripting is protecting commands from variables that might expand to strings starting with a hyphen. Without --, a variable expansion could inadvertently inject malicious or invalid options into a command.

# Vulnerable: If $filename evaluates to "-rf", rm interprets it as an option.
rm "$filename"


# Secure: The -- operator forces $filename to be evaluated as a target operand.
rm -- "$filename"
grep -- "$pattern" file.txt

Interaction with Bash Built-ins

The -- operator is deeply integrated into Bash built-in commands, most notably set, which manipulates the shell’s internal positional parameters ($1, $2, etc.). Assigning literal parameters:
set -- -a -b -c
In this construct, -- prevents set from interpreting -a, -b, and -c as shell attributes (like set -e or set -x). Instead, it assigns them directly to $1, $2, and $3. Clearing parameters:
set --
When -- is passed to set with no subsequent operands, it flushes the current context’s positional parameters, unsetting $1, $2, and $@.
Master Bash with Deep Grasping Methodology!Learn More