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.

getopts is a POSIX-compliant shell builtin used to parse short command-line options and their associated arguments. It iterates over positional parameters sequentially, extracting valid options defined by a format string and managing the internal state required for parsing. Note that getopts strictly processes short options (e.g., -a, -b) and does not natively support long options (e.g., --alpha).
getopts optstring name [args]

Parameters

  • optstring: A string containing the legitimate option characters to be recognized.
    • If a character is followed by a colon (:), the option requires an argument.
    • If the string begins with a colon (:), getopts operates in silent error reporting mode.
  • name: The name of the shell variable where the currently parsed option character is stored.
  • args: (Optional) A specific array of arguments to parse. If omitted, getopts defaults to parsing the script or function’s positional parameters ("$@").

Internal State Variables

getopts relies on three internal shell variables to track state and store data during execution:
  • OPTIND (Option Index): The index of the next argument to be processed. It is initialized to 1 upon shell invocation. If you need to parse a new set of parameters within the same shell session, OPTIND must be manually reset to 1.
  • OPTARG (Option Argument): Stores the value of the argument associated with the current option, provided the option was defined in the optstring as requiring an argument.
  • OPTERR (Option Error): A boolean variable (default 1) that controls whether Bash prints its own diagnostic error messages. Setting OPTERR=0 disables default shell error output, though silent mode (via optstring) is generally preferred for custom error handling.

Error Handling Mechanics

The behavior of getopts when encountering an invalid option or a missing argument depends entirely on whether it is operating in Verbose Mode or Silent Mode.

Verbose Mode (Default)

Triggered when optstring does not begin with a colon.
  • Invalid Option: getopts assigns the literal ? character to the name variable, unsets OPTARG, and prints a standard shell error message to stderr.
  • Missing Argument: getopts assigns the literal ? character to the name variable, unsets OPTARG, and prints a standard shell error message to stderr.

Silent Mode

Triggered when optstring begins with a colon (e.g., :a:bc).
  • Invalid Option: getopts assigns the literal ? character to the name variable, assigns the invalid option character itself to OPTARG, and suppresses all shell error messages.
  • Missing Argument: getopts assigns the literal : character to the name variable, assigns the option character that lacked an argument to OPTARG, and suppresses all shell error messages.

Execution Flow and Exit Status

getopts is designed to be evaluated within a while loop.
  • Success (0): Returned each time an option is successfully parsed, regardless of whether the option is valid or invalid according to the optstring.
  • Failure (>0): Returned when getopts reaches the end of the options. This occurs when it encounters the first non-option argument (a string not starting with -), the explicit end-of-options delimiter (--), or the end of the positional parameters. Upon returning failure, name is set to ?.
Master Bash with Deep Grasping Methodology!Learn More