LinuxCommandLibrary

getopt

Parse command-line options

TLDR

Parse optional verbose/version flags with shorthands

$ getopt [[-o|--options]] vV [[-l|--longoptions]] verbose,version -- --version --verbose
copy

Add a --file option with a required argument with shorthand -f
$ getopt [[-o|--options]] f: [[-l|--longoptions]] file: -- --file=somefile
copy

Add a --verbose option with an optional argument with shorthand -v, and pass a non-option parameter arg
$ getopt [[-o|--options]] v:: [[-l|--longoptions]] verbose:: -- --verbose arg
copy

Accept a -r and --verbose flag, a --accept option with an optional argument and add a --target with a required argument option with shorthands
$ getopt [[-o|--options]] rv::s::t: [[-l|--longoptions]] verbose,source::,target: -- -v --target target
copy

SYNOPSIS

getopt [options] [--]

PARAMETERS

-o shortopts
    Short Options.
Specifies a string of valid short option characters. If a character is followed by a colon (:), the option requires an argument. If followed by two colons (::), the option takes an optional argument.

-l longopts
    Long Options.
Specifies a comma-separated list of valid long option names. If an option name is followed by a colon (:), it requires an argument. If followed by two colons (::), it takes an optional argument.

-n progname
    Program Name.
The program name to use in error messages. Defaults to getopt.

-q, --quiet
    Quiet Mode.
Suppresses error messages (e.g., for unrecognized options).

-u, --unquoted
    Unquoted Output.
Do not quote the output. (Dangerous for arguments with spaces or shell meta-characters).

-s shell
    Shell Quoting Style.
Sets the quoting style for the output. Typically sh (default) or csh.

-V, --version
    Version Information.
Print version information and exit.

-h, --help
    Help Information.
Print help message and exit.

DESCRIPTION

getopt is a utility designed to parse command-line options and parameters for shell scripts. It reorders the arguments so that options are processed first, followed by non-option arguments, and properly handles quoted arguments. This allows scripts to use standard option parsing conventions (e.g., -a, --long-option).

The command generates a reformatted argument string that can be used with eval set -- "$(getopt ...)". This is particularly useful for robust script development, as it simplifies the parsing logic and ensures compatibility with standard UNIX option conventions. It supports both short options (e.g., -f) and long options (e.g., --file). Unlike the getopts shell builtin, getopt is an external command and can handle long options and complex argument reordering.

It is crucial for scripts that need to adhere to standard command-line interfaces, allowing them to accept options with or without arguments, and to separate them from positional parameters gracefully.

CAVEATS

getopt is an external command, requiring a subprocess call, which can be slightly slower than shell builtins. For simpler cases, the getopts shell builtin is often preferred, but it does not support long options.

The output of getopt must typically be evaluated and set -- to correctly re-assign the shell's positional parameters, which can be a source of quoting issues if not handled carefully, especially with the original getopt. GNU getopt is much more robust in this regard.

The original getopt utility had limitations with arguments containing spaces; GNU getopt (common on Linux) largely overcomes this with proper quoting.

TYPICAL USAGE PATTERN

Shell scripts commonly use getopt in conjunction with eval and set -- to parse arguments. A typical pattern is:

TEMP=$(getopt -o "a:bc" -l "alpha:,beta,gamma" -n "$0" -- "$@")
if [ $? -ne 0 ]; then exit 1; fi
eval set -- "$TEMP"
while true; do
case "$1" in
-a|--alpha)
ARG_A="$2"; shift 2;;
-b|--beta)
ARG_B=true; shift;;
-c|--gamma)
ARG_C=true; shift;;
--) shift; break;;
*) echo "Internal error!"; exit 1;;
esac
done

This pattern safely reorders and parses arguments, making subsequent case statements straightforward.

HISTORY

The original getopt utility was developed for Unix systems to standardize command-line argument parsing. It had limitations, particularly with complex arguments and quoting. The more robust GNU getopt was developed as part of the GNU project to address these shortcomings, notably by providing better handling of spaces in arguments and supporting long options. Modern Linux distributions almost exclusively use the GNU version, which has become a de-facto standard for parsing options in shell scripts due to its power and flexibility compared to its predecessor or the simpler getopts shell builtin.

SEE ALSO

getopts(1), eval(1), set(1), sh(1)

Copied to clipboard