{
    "mode": "man",
    "parameter": "GETOPT",
    "section": "1",
    "url": "https://www.chedong.com/phpMan.php/man/GETOPT/1/json",
    "generated": "2026-06-13T21:20:58Z",
    "synopsis": "getopt optstring parameters getopt [options] [--] optstring parameters getopt [options]",
    "sections": {
        "NAME": {
            "content": "getopt - parse command options (enhanced)\n",
            "subsections": []
        },
        "SYNOPSIS": {
            "content": "getopt optstring parameters getopt [options] [--] optstring parameters getopt [options]",
            "subsections": [
                {
                    "name": "-o --options --",
                    "content": "",
                    "flag": "-o",
                    "long": "--options"
                }
            ]
        },
        "DESCRIPTION": {
            "content": "getopt is used to break up (parse) options in command lines for easy parsing by shell\nprocedures, and to check for valid options. It uses the GNU getopt(3) routines to do this.\n\nThe parameters getopt is called with can be divided into two parts: options which modify the\nway getopt will do the parsing (the options and the optstring in the SYNOPSIS), and the\nparameters which are to be parsed (parameters in the SYNOPSIS). The second part will start at\nthe first non-option parameter that is not an option argument, or after the first occurrence\nof '--'. If no '-o' or '--options' option is found in the first part, the first parameter of\nthe second part is used as the short options string.\n\nIf the environment variable GETOPTCOMPATIBLE is set, or if the first parameter is not an\noption (does not start with a '-', the first format in the SYNOPSIS), getopt will generate\noutput that is compatible with that of other versions of getopt(1). It will still do\nparameter shuffling and recognize optional arguments (see section COMPATIBILITY for more\ninformation).\n\nTraditional implementations of getopt(1) are unable to cope with whitespace and other\n(shell-specific) special characters in arguments and non-option parameters. To solve this\nproblem, this implementation can generate quoted output which must once again be interpreted\nby the shell (usually by using the eval command). This has the effect of preserving those\ncharacters, but you must call getopt in a way that is no longer compatible with other\nversions (the second or third format in the SYNOPSIS). To determine whether this enhanced\nversion of getopt(1) is installed, a special test option (-T) can be used.\n",
            "subsections": []
        },
        "OPTIONS": {
            "content": "",
            "subsections": [
                {
                    "name": "-a --alternative",
                    "content": "Allow long options to start with a single '-'.\n",
                    "flag": "-a",
                    "long": "--alternative"
                },
                {
                    "name": "-h --help",
                    "content": "Display help text and exit. No other output is generated.\n",
                    "flag": "-h",
                    "long": "--help"
                },
                {
                    "name": "-l --longoptions",
                    "content": "The long (multi-character) options to be recognized. More than one option name may be\nspecified at once, by separating the names with commas. This option may be given more\nthan once, the longopts are cumulative. Each long option name in longopts may be followed\nby one colon to indicate it has a required argument, and by two colons to indicate it has\nan optional argument.\n",
                    "flag": "-l",
                    "long": "--longoptions"
                },
                {
                    "name": "-n --name",
                    "content": "The name that will be used by the getopt(3) routines when it reports errors. Note that\nerrors of getopt(1) are still reported as coming from getopt.\n",
                    "flag": "-n",
                    "long": "--name"
                },
                {
                    "name": "-o --options",
                    "content": "The short (one-character) options to be recognized. If this option is not found, the\nfirst parameter of getopt that does not start with a '-' (and is not an option argument)\nis used as the short options string. Each short option character in shortopts may be\nfollowed by one colon to indicate it has a required argument, and by two colons to\nindicate it has an optional argument. The first character of shortopts may be '+' or '-'\nto influence the way options are parsed and output is generated (see section SCANNING\nMODES for details).\n",
                    "flag": "-o",
                    "long": "--options"
                },
                {
                    "name": "-q --quiet",
                    "content": "Disable error reporting by getopt(3).\n",
                    "flag": "-q",
                    "long": "--quiet"
                },
                {
                    "name": "-Q --quiet-output",
                    "content": "Do not generate normal output. Errors are still reported by getopt(3), unless you also\nuse -q.\n",
                    "flag": "-Q",
                    "long": "--quiet-output"
                },
                {
                    "name": "-s --shell",
                    "content": "Set quoting conventions to those of shell. If the -s option is not given, the BASH\nconventions are used. Valid arguments are currently 'sh' 'bash', 'csh', and 'tcsh'.\n",
                    "flag": "-s",
                    "long": "--shell"
                },
                {
                    "name": "-T --test",
                    "content": "Test if your getopt(1) is this enhanced version or an old version. This generates no\noutput, and sets the error status to 4. Other implementations of getopt(1), and this\nversion if the environment variable GETOPTCOMPATIBLE is set, will return '--' and error\nstatus 0.\n",
                    "flag": "-T",
                    "long": "--test"
                },
                {
                    "name": "-u --unquoted",
                    "content": "Do not quote the output. Note that whitespace and special (shell-dependent) characters\ncan cause havoc in this mode (like they do with other getopt(1) implementations).\n",
                    "flag": "-u",
                    "long": "--unquoted"
                },
                {
                    "name": "-V --version",
                    "content": "Display version information and exit. No other output is generated.\n",
                    "flag": "-V",
                    "long": "--version"
                }
            ]
        },
        "PARSING": {
            "content": "This section specifies the format of the second part of the parameters of getopt (the\nparameters in the SYNOPSIS). The next section (OUTPUT) describes the output that is\ngenerated. These parameters were typically the parameters a shell function was called with.\nCare must be taken that each parameter the shell function was called with corresponds to\nexactly one parameter in the parameter list of getopt (see the EXAMPLES). All parsing is done\nby the GNU getopt(3) routines.\n\nThe parameters are parsed from left to right. Each parameter is classified as a short option,\na long option, an argument to an option, or a non-option parameter.\n\nA simple short option is a '-' followed by a short option character. If the option has a\nrequired argument, it may be written directly after the option character or as the next\nparameter (i.e., separated by whitespace on the command line). If the option has an optional\nargument, it must be written directly after the option character if present.\n\nIt is possible to specify several short options after one '-', as long as all (except\npossibly the last) do not have required or optional arguments.\n\nA long option normally begins with '--' followed by the long option name. If the option has a\nrequired argument, it may be written directly after the long option name, separated by '=',\nor as the next argument (i.e., separated by whitespace on the command line). If the option\nhas an optional argument, it must be written directly after the long option name, separated\nby '=', if present (if you add the '=' but nothing behind it, it is interpreted as if no\nargument was present; this is a slight bug, see the BUGS). Long options may be abbreviated,\nas long as the abbreviation is not ambiguous.\n\nEach parameter not starting with a '-', and not a required argument of a previous option, is\na non-option parameter. Each parameter after a '--' parameter is always interpreted as a\nnon-option parameter. If the environment variable POSIXLYCORRECT is set, or if the short\noption string started with a '+', all remaining parameters are interpreted as non-option\nparameters as soon as the first non-option parameter is found.\n",
            "subsections": []
        },
        "OUTPUT": {
            "content": "Output is generated for each element described in the previous section. Output is done in the\nsame order as the elements are specified in the input, except for non-option parameters.\nOutput can be done in compatible (unquoted) mode, or in such way that whitespace and other\nspecial characters within arguments and non-option parameters are preserved (see QUOTING).\nWhen the output is processed in the shell script, it will seem to be composed of distinct\nelements that can be processed one by one (by using the shift command in most shell\nlanguages). This is imperfect in unquoted mode, as elements can be split at unexpected places\nif they contain whitespace or special characters.\n\nIf there are problems parsing the parameters, for example because a required argument is not\nfound or an option is not recognized, an error will be reported on stderr, there will be no\noutput for the offending element, and a non-zero error status is returned.\n\nFor a short option, a single '-' and the option character are generated as one parameter. If\nthe option has an argument, the next parameter will be the argument. If the option takes an\noptional argument, but none was found, the next parameter will be generated but be empty in\nquoting mode, but no second parameter will be generated in unquoted (compatible) mode. Note\nthat many other getopt(1) implementations do not support optional arguments.\n\nIf several short options were specified after a single '-', each will be present in the\noutput as a separate parameter.\n\nFor a long option, '--' and the full option name are generated as one parameter. This is done\nregardless whether the option was abbreviated or specified with a single '-' in the input.\nArguments are handled as with short options.\n\nNormally, no non-option parameters output is generated until all options and their arguments\nhave been generated. Then '--' is generated as a single parameter, and after it the\nnon-option parameters in the order they were found, each as a separate parameter. Only if the\nfirst character of the short options string was a '-', non-option parameter output is\ngenerated at the place they are found in the input (this is not supported if the first format\nof the SYNOPSIS is used; in that case all preceding occurrences of '-' and '+' are ignored).\n",
            "subsections": []
        },
        "QUOTING": {
            "content": "In compatibility mode, whitespace or 'special' characters in arguments or non-option\nparameters are not handled correctly. As the output is fed to the shell script, the script\ndoes not know how it is supposed to break the output into separate parameters. To circumvent\nthis problem, this implementation offers quoting. The idea is that output is generated with\nquotes around each parameter. When this output is once again fed to the shell (usually by a\nshell eval command), it is split correctly into separate parameters.\n\nQuoting is not enabled if the environment variable GETOPTCOMPATIBLE is set, if the first\nform of the SYNOPSIS is used, or if the option '-u' is found.\n\nDifferent shells use different quoting conventions. You can use the '-s' option to select the\nshell you are using. The following shells are currently supported: 'sh', 'bash', 'csh' and\n'tcsh'. Actually, only two 'flavors' are distinguished: sh-like quoting conventions and\ncsh-like quoting conventions. Chances are that if you use another shell script language, one\nof these flavors can still be used.\n",
            "subsections": []
        },
        "SCANNING MODES": {
            "content": "The first character of the short options string may be a '-' or a '+' to indicate a special\nscanning mode. If the first calling form in the SYNOPSIS is used they are ignored; the\nenvironment variable POSIXLYCORRECT is still examined, though.\n\nIf the first character is '+', or if the environment variable POSIXLYCORRECT is set, parsing\nstops as soon as the first non-option parameter (i.e., a parameter that does not start with a\n'-') is found that is not an option argument. The remaining parameters are all interpreted as\nnon-option parameters.\n\nIf the first character is a '-', non-option parameters are outputted at the place where they\nare found; in normal operation, they are all collected at the end of output after a '--'\nparameter has been generated. Note that this '--' parameter is still generated, but it will\nalways be the last parameter in this mode.\n",
            "subsections": []
        },
        "COMPATIBILITY": {
            "content": "This version of getopt(1) is written to be as compatible as possible to other versions.\nUsually you can just replace them with this version without any modifications, and with some\nadvantages.\n\nIf the first character of the first parameter of getopt is not a '-', getopt goes into\ncompatibility mode. It will interpret its first parameter as the string of short options, and\nall other arguments will be parsed. It will still do parameter shuffling (i.e., all\nnon-option parameters are output at the end), unless the environment variable POSIXLYCORRECT\nis set, in which case, getopt will prepend a '+' before short options automatically.\n\nThe environment variable GETOPTCOMPATIBLE forces getopt into compatibility mode. Setting\nboth this environment variable and POSIXLYCORRECT offers 100% compatibility for 'difficult'\nprograms. Usually, though, neither is needed.\n\nIn compatibility mode, leading '-' and '+' characters in the short options string are\nignored.\n",
            "subsections": []
        },
        "RETURN CODES": {
            "content": "getopt returns error code 0 for successful parsing, 1 if getopt(3) returns errors, 2 if it\ndoes not understand its own parameters, 3 if an internal error occurs like out-of-memory, and\n4 if it is called with -T.\n",
            "subsections": []
        },
        "EXAMPLES": {
            "content": "Example scripts for (ba)sh and (t)csh are provided with the getopt(1) distribution, and are\ninstalled in /usr/share/doc/util-linux/examples/ directory.\n",
            "subsections": []
        },
        "ENVIRONMENT": {
            "content": "POSIXLYCORRECT\nThis environment variable is examined by the getopt(3) routines. If it is set, parsing\nstops as soon as a parameter is found that is not an option or an option argument. All\nremaining parameters are also interpreted as non-option parameters, regardless whether\nthey start with a '-'.\n\nGETOPTCOMPATIBLE\nForces getopt to use the first calling format as specified in the SYNOPSIS.\n",
            "subsections": []
        },
        "BUGS": {
            "content": "getopt(3) can parse long options with optional arguments that are given an empty optional\nargument (but cannot do this for short options). This getopt(1) treats optional arguments\nthat are empty as if they were not present.\n\nThe syntax if you do not want any short option variables at all is not very intuitive (you\nhave to set them explicitly to the empty string).\n",
            "subsections": []
        },
        "AUTHOR": {
            "content": "Frodo Looijaard <frodo@frodo.looijaard.name>\n",
            "subsections": []
        },
        "SEE ALSO": {
            "content": "bash(1), tcsh(1), getopt(3)\n",
            "subsections": []
        },
        "REPORTING BUGS": {
            "content": "For bug reports, use the issue tracker at https://github.com/karelzak/util-linux/issues.\n",
            "subsections": []
        },
        "AVAILABILITY": {
            "content": "The getopt command is part of the util-linux package which can be downloaded from Linux\nKernel Archive <https://www.kernel.org/pub/linux/utils/util-linux/>.\n\n\n\nutil-linux 2.37.2                            2021-06-02                                    GETOPT(1)",
            "subsections": []
        }
    },
    "summary": "getopt - parse command options (enhanced)",
    "flags": [
        {
            "flag": "-a",
            "long": "--alternative",
            "arg": null,
            "description": "Allow long options to start with a single '-'."
        },
        {
            "flag": "-h",
            "long": "--help",
            "arg": null,
            "description": "Display help text and exit. No other output is generated."
        },
        {
            "flag": "-l",
            "long": "--longoptions",
            "arg": null,
            "description": "The long (multi-character) options to be recognized. More than one option name may be specified at once, by separating the names with commas. This option may be given more than once, the longopts are cumulative. Each long option name in longopts may be followed by one colon to indicate it has a required argument, and by two colons to indicate it has an optional argument."
        },
        {
            "flag": "-n",
            "long": "--name",
            "arg": null,
            "description": "The name that will be used by the getopt(3) routines when it reports errors. Note that errors of getopt(1) are still reported as coming from getopt."
        },
        {
            "flag": "-o",
            "long": "--options",
            "arg": null,
            "description": "The short (one-character) options to be recognized. If this option is not found, the first parameter of getopt that does not start with a '-' (and is not an option argument) is used as the short options string. Each short option character in shortopts may be followed by one colon to indicate it has a required argument, and by two colons to indicate it has an optional argument. The first character of shortopts may be '+' or '-' to influence the way options are parsed and output is generated (see section SCANNING MODES for details)."
        },
        {
            "flag": "-q",
            "long": "--quiet",
            "arg": null,
            "description": "Disable error reporting by getopt(3)."
        },
        {
            "flag": "-Q",
            "long": "--quiet-output",
            "arg": null,
            "description": "Do not generate normal output. Errors are still reported by getopt(3), unless you also use -q."
        },
        {
            "flag": "-s",
            "long": "--shell",
            "arg": null,
            "description": "Set quoting conventions to those of shell. If the -s option is not given, the BASH conventions are used. Valid arguments are currently 'sh' 'bash', 'csh', and 'tcsh'."
        },
        {
            "flag": "-T",
            "long": "--test",
            "arg": null,
            "description": "Test if your getopt(1) is this enhanced version or an old version. This generates no output, and sets the error status to 4. Other implementations of getopt(1), and this version if the environment variable GETOPTCOMPATIBLE is set, will return '--' and error status 0."
        },
        {
            "flag": "-u",
            "long": "--unquoted",
            "arg": null,
            "description": "Do not quote the output. Note that whitespace and special (shell-dependent) characters can cause havoc in this mode (like they do with other getopt(1) implementations)."
        },
        {
            "flag": "-V",
            "long": "--version",
            "arg": null,
            "description": "Display version information and exit. No other output is generated."
        }
    ],
    "examples": [
        "Example scripts for (ba)sh and (t)csh are provided with the getopt(1) distribution, and are",
        "installed in /usr/share/doc/util-linux/examples/ directory."
    ],
    "see_also": [
        {
            "name": "bash",
            "section": "1",
            "url": "https://www.chedong.com/phpMan.php/man/bash/1/json"
        },
        {
            "name": "tcsh",
            "section": "1",
            "url": "https://www.chedong.com/phpMan.php/man/tcsh/1/json"
        },
        {
            "name": "getopt",
            "section": "3",
            "url": "https://www.chedong.com/phpMan.php/man/getopt/3/json"
        }
    ],
    "tldr": {
        "source": "official",
        "description": "Parse command-line arguments.",
        "examples": [
            {
                "description": "Parse optional `verbose`/`version` flags with shorthands",
                "command": "getopt {{-o|--options}} vV {{-l|--longoptions}} verbose,version -- --version --verbose"
            },
            {
                "description": "Add a `--file` option with a required argument with shorthand `-f`",
                "command": "getopt {{-o|--options}} f: {{-l|--longoptions}} file: -- --file=somefile"
            },
            {
                "description": "Add a `--verbose` option with an optional argument with shorthand `-v`, and pass a non-option parameter `arg`",
                "command": "getopt {{-o|--options}} v:: {{-l|--longoptions}} verbose:: -- --verbose arg"
            },
            {
                "description": "Accept a `-r` and `--verbose` flag, a `--accept` option with an optional argument and add a `--target` with a required argument option with shorthands",
                "command": "getopt {{-o|--options}} rv::s::t: {{-l|--longoptions}} verbose,source::,target: -- -v --target target"
            }
        ]
    }
}