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 [-o optstring] [-l longopts[,helpfile]] [-n name] [-q] [-s sh|bash|ksh|zsh] [-T] [-u] [-a] [--] [parameters]

PARAMETERS

-a, --alternative
    Allow long options starting with single -

-h, --help
    Display usage information and exit

-l, --longoptions longopts[,helpfile]
    Comma-separated long options; : for required arg, + for optional

-n, --name progname
    Name to use in error messages

-o, --options optstring
    Short options; : for required arg

-q, --quiet
    Suppress error reporting

-s, --shell sh|bash|ksh|zsh
    Quoting conventions for specified shell

-T, --test
    Test for getopt(3) version (returns true)

-u, --unquoted
    Output unquoted parameters

--
    Terminate option list

DESCRIPTION

getopt is a utility for parsing and standardizing command-line arguments in shell scripts. It processes options (short like -v or long like --verbose) and positional parameters, outputting them in a canonical form: options first (with arguments attached where needed), followed by --, then remaining arguments. This enables easy processing via a while loop and case statement after resetting positional parameters with eval set -- "$(getopt ...)".

The traditional form uses an optstring where letters denote options and : marks required arguments (e.g., ab:c for -a, -b, -c arg). Modern util-linux getopt adds long options, optional arguments (via +), bundling (-abc), and POSIX compliance. It respects quoting, handles spaces in arguments, and supports alternative modes.

Common in scripts for portability over getopts (builtin, no long options). Caveat: output must be double-quoted to preserve spaces. Widely used since early Unix for robust option handling.

CAVEATS

Always quote getopt output: eval set -- "$(getopt ...)". Original BSD getopt lacks long options, has quoting bugs. Not for interactive use. Fails on unhandled options unless -q.

TYPICAL USAGE

args=$(getopt -o hl: -l help,longtest: -- "$@")
eval set -- "$args"
while true; do
case "$1" in
-h|--help) echo "Help"; shift;;
-l|--longtest) L="$2"; shift 2;;
--) shift; break;;
*) break;;
esac
done

ENVIRONMENT

POSIXLY_CORRECT: Enforce strict POSIX rules (no bundling after first non-option).
SHELL: Default shell for quoting if -s unset.

HISTORY

Appeared in Version 7 Unix (1979). Enhanced with long options by Hans-Dieter Pinzer in 1997 for util-linux; now standard in Linux distributions.

SEE ALSO

getopts(1), getopt(3)

Copied to clipboard