GETOPT(1)

๐Ÿ’– GETOPT(1) ๐Ÿ’–

okay so this is the manual page for getopt โ€” that's the User Commands one, bestie, not the C library thing (iykyk). Let's get into it ๐Ÿซถ


โœจ NAME โœจ

getopt โ€” parse command options, but like, the enhanced version ๐Ÿ’…


๐ŸŒธ SYNOPSIS ๐ŸŒธ

getopt optstring parameters
getopt [options] [--] optstring parameters
getopt [options] -o|--options optstring [options] [--] parameters

๐Ÿฆ‹ DESCRIPTION ๐Ÿฆ‹

So here's the vibe: getopt breaks up (parses) the options in command lines so your shell procedures can deal with them without losing their minds, and it also checks that the options are actually valid. It does all this by using the GNU getopt(3) routines underneath. no cap.

The parameters you call getopt with split into two parts, and this is the part where everyone gets confused so pay attention:

  • the options that change how getopt does its parsing โ€” that's the options and the optstring in the SYNOPSIS
  • the parameters that are gonna get parsed โ€” that's parameters in the SYNOPSIS

That second part starts at the first non-option parameter that isn't an option argument, or right after the first time you hit --. And if there's no -o or --options option hanging out in the first part, then the first parameter of the second part gets used as the short options string. iconic behavior honestly.

If the environment variable GETOPT_COMPATIBLE is set, or if the first parameter isn't an option (like, it doesn't start with a - โ€” that's the first format in the SYNOPSIS), then getopt will generate output that's compatible with what other versions of getopt(1) spit out. It'll still do parameter shuffling and recognize optional arguments though โ€” check the COMPATIBILITY section for the details.

Traditional implementations of getopt(1) literally cannot cope with whitespace and other (shell-specific) special characters in arguments and non-option parameters. ๐Ÿ˜ญ To fix that whole situation, this implementation can generate quoted output which has to be interpreted by the shell again (usually by using the eval command). The effect is that those characters get preserved โ€” but you have to call getopt in a way that's no longer compatible with other versions (that's the second or third format in the SYNOPSIS). And if you wanna know whether this enhanced version of getopt(1) is even installed on your machine, there's a special test option (-T) for exactly that. ๐Ÿ’…


๐Ÿ‘‘ OPTIONS ๐Ÿ‘‘

  • -a, --alternative โ€” let long options start with a single -.
  • -l, --longoptions longopts โ€” the long (multi-character) options to be recognized. You can drop more than one option name at once by separating the names with commas or whitespace (spaces, tabs, or newlines). This option can be given more than once โ€” the longopts are cumulative, they stack. Each long option name in longopts may be followed by one colon to say it has a required argument, and by two colons to say it has an optional argument.
  • -n, --name progname โ€” the name that the getopt(3) routines will use when they report errors. Heads up though: errors from getopt(1) itself are still reported as coming from getopt.
  • -o, --options shortopts โ€” the short (one-character) options to be recognized. If this option isn't found, the first parameter of getopt that doesn't start with a - (and isn't an option argument) gets used as the short options string. Each short option character in shortopts may be followed by one colon for a required argument, and two colons for an optional argument. Also, the first character of shortopts may be + or - to influence how options are parsed and how output is generated โ€” see the SCANNING MODES section for the tea.
  • -q, --quiet โ€” disable error reporting by getopt(3).
  • -Q, --quiet-output โ€” don't generate normal output. Errors are still reported by getopt(3), unless you also use -q.
  • -s, --shell shell โ€” set the quoting conventions to those of shell. If the -s option isn't given, the BASH conventions are used. Valid arguments right now are sh, bash, csh, and tcsh.
  • -T, --test โ€” test whether your getopt(1) is this enhanced version or an old dusty one. This generates no output at all, and sets the error status to 4. Other implementations of getopt(1) โ€” and this version too, if the environment variable GETOPT_COMPATIBLE is set โ€” will return -- and error status 0.
  • -u, --unquoted โ€” do not quote the output. And ngl, whitespace and special (shell-dependent) characters can cause absolute havoc in this mode (like they do with other getopt(1) implementations). you've been warned ๐Ÿ’€
  • -U, --unknown โ€” leave unknown options as they are and suppress error messages from getopt(3). Since there's no way to know whether an unknown option requires an argument, a non-option argument that follows the unknown option after a whitespace is considered an option argument โ€” so the argument gets left untouched and printed next to the respective unknown option. To prevent unexpected behavior, short options should be specified individually.
  • -h, --help โ€” display help text and exit.
  • -V, --version โ€” display version and exit.

๐Ÿ”ฅ PARSING ๐Ÿ”ฅ

This section specifies the format of the second part of the parameters of getopt (the parameters in the SYNOPSIS). The next section (OUTPUT) describes the output that gets generated. These parameters were typically the parameters a shell function was called with. And care must be taken that each parameter the shell function was called with corresponds to exactly one parameter in the parameter list of getopt (peep the EXAMPLES). All parsing is done by the GNU getopt(3) routines. periodt.

The parameters are parsed from left to right. Each parameter gets classified as one of these:

  • a short option
  • a long option
  • an argument to an option
  • a non-option parameter

A simple short option is a - followed by a short option character โ€” except for the :, ; and ? characters, because those are reserved by getopt(3). If the option has a required argument, it may be written directly after the option character, or as the next parameter (i.e., separated by whitespace on the command line). If the option has an optional argument, it must be written directly after the option character if it's present.

You can specify several short options after one -, as long as all of them (except possibly the last) don't have required or optional arguments. slay.

A long option normally begins with -- followed by the long option name. If the option has a required argument, it may be written directly after the long option name separated by =, or as the next argument (i.e., separated by whitespace on the command line). If the option has an optional argument, it must be written directly after the long option name, separated by =, if present. (And if you add the = but put nothing behind it, it's interpreted as if no argument was present โ€” yeah that's a slight bug, see the BUGS section.) Long options may be abbreviated, as long as the abbreviation isn't ambiguous.

Each parameter that doesn't start with a -, and isn't a required argument of a previous option, is a non-option parameter. And each parameter after a -- parameter is always interpreted as a non-option parameter. If the environment variable POSIXLY_CORRECT is set, or if the short option string started with a +, then all remaining parameters are interpreted as non-option parameters the second the first non-option parameter is found. no takebacks.


๐ŸŒˆ OUTPUT ๐ŸŒˆ

Output is generated for each element described in the previous section. It's done in the same order as the elements are specified in the input โ€” except for non-option parameters, those are the exception bestie. Output can be done in compatible (unquoted) mode, or in such a way that whitespace and other special characters within arguments and non-option parameters are preserved (see QUOTING). When the output is processed in the shell script, it'll seem to be composed of distinct elements that can be processed one by one (by using the shift command in most shell languages). This is imperfect in unquoted mode though, because elements can get split at unexpected places if they contain whitespace or special characters. ๐Ÿ™ˆ

If there are problems parsing the parameters โ€” like a required argument isn't found, or an option isn't recognized โ€” an error gets reported on stderr, there will be no output for the offending element, and a non-zero error status is returned.

For a short option, a single - and the option character are generated as one parameter. If the option has an argument, the next parameter will be the argument. If the option takes an optional argument but none was found, the next parameter will be generated but be empty in quoting mode โ€” while in unquoted (compatible) mode, no second parameter gets generated at all. And note: many other getopt(1) implementations do not support optional arguments. ๐Ÿ˜Œ

If several short options were specified after a single -, each one will be present in the output as a separate parameter.

For a long option, -- and the full option name are generated as one parameter. This happens regardless of whether the option was abbreviated or specified with a single - in the input. Arguments are handled the same as with short options.

Normally, no non-option parameters output is generated until all options and their arguments have been generated. Then -- is generated as a single parameter, and after it come the non-option parameters in the order they were found, each as a separate parameter. The only exception: if the first character of the short options string was a -, then non-option parameter output is generated at the place they're found in the input. (And that's not supported if the first format of the SYNOPSIS is used โ€” in that case all preceding occurrences of - and + are ignored.)


๐Ÿ’œ QUOTING ๐Ÿ’œ

In compatibility mode, whitespace or 'special' characters in arguments or non-option parameters are not handled correctly. Because the output gets fed to the shell script, the script has no idea how it's supposed to break the output into separate parameters. To get around this whole problem, this implementation offers quoting. The idea is simple: output gets generated with quotes around each parameter. Then when that output is fed to the shell again (usually by a shell eval command), it splits correctly into separate parameters. ๐ŸŽ€

Quoting is not enabled if:

  • the environment variable GETOPT_COMPATIBLE is set,
  • the first form of the SYNOPSIS is used, or
  • the option -u is found.

Different shells use different quoting conventions, so you can use the -s option to pick the shell you're using. These are the shells currently supported: sh, bash, csh and tcsh. But actually โ€” only two 'flavors' are distinguished: sh-like quoting conventions and csh-like quoting conventions. Chances are, if you're using some other shell script language, one of these flavors will still work for you. โœจ


๐ŸŒธ part 1 done โ€” part 2 is coming with the rest, don't go anywhere bestie ๐Ÿ’–

๐ŸŒ€ SCANNING MODES ๐ŸŒ€

Okay bestie, plot twist time: the very first character of your short options string can be a - or a +, and that little guy is doing a LOT of heavy lifting โ€” it switches up the whole scanning mode. ๐Ÿ’…

  • If you're using the first calling form in the SYNOPSIS, those leading characters get ignored, bestie. Not important. But โ€” and this is the sneaky part โ€” the environment variable POSIXLY_CORRECT is still examined. So it's not like you get a free pass, okurrr. ๐Ÿ™ˆ
  • If the first character is +, or if the environment variable POSIXLY_CORRECT is set, parsing just stops the moment it finds the first non-option parameter (like, a parameter that doesn't start with a -) that isn't an option argument. Everything left over after that? All of it gets interpreted as non-option parameters. That's the whole rest of the line, no cap. โœ‹
  • If the first character is -, non-option parameters get outputted right where they're found. Whereas in normal operation they all get collected at the end of output, after a -- parameter has been generated. And yes, that -- parameter is still generated in this mode too โ€” it's just always gonna be the very last parameter. Periodt. ๐ŸŽ€

๐Ÿค COMPATIBILITY ๐Ÿค

This version of getopt(1) is written to be as compatible as possible with other versions. Usually you can just swap them out with this one, no modifications, and get some advantages out of it. Slay. โœจ

If the first character of the first parameter of getopt is not a -, getopt goes into compatibility mode. It'll interpret its first parameter as the string of short options, and all the other arguments get parsed. It will still do parameter shuffling (meaning all non-option parameters are output at the end) โ€” unless the environment variable POSIXLY_CORRECT is set, in which case getopt will prepend a + before short options automatically. Look at it being helpful. ๐Ÿซถ

The environment variable GETOPT_COMPATIBLE forces getopt into compatibility mode. Setting both this environment variable and POSIXLY_CORRECT offers 100% compatibility for 'difficult' programs. Usually though? Neither is needed. Chill. ๐Ÿ˜Œ

In compatibility mode, leading - and + characters in the short options string are ignored. Gone. Bye. ๐Ÿ’€


๐Ÿšจ RETURN CODES ๐Ÿšจ

Listen up, because these matter:

  • 0 โ€” successful parsing, we ate that ๐Ÿ’…
  • 1 โ€” getopt(3) returns errors
  • 2 โ€” it does not understand its own parameters (embarrassing, honestly)
  • 3 โ€” an internal error occurs, like out-of-memory ๐Ÿ˜ญ
  • 4 โ€” it is called with -T

๐ŸŒˆ EXAMPLES ๐ŸŒˆ

Example scripts for (ba)sh and (t)csh are provided with the getopt(1) distribution, and they're installed in /usr/share/doc/util-linux. Go look, bestie, they're right there. ๐ŸŽ€


๐ŸŒธ ENVIRONMENT ๐ŸŒธ

  • POSIXLY_CORRECT โ€” This environment variable is examined by the getopt(3) routines. If it's set, parsing stops as soon as a parameter is found that is not an option or an option argument. All remaining parameters are also interpreted as non-option parameters, regardless of whether they start with a -. Yes, even then. It does not care. ๐Ÿ˜ค
  • GETOPT_COMPATIBLE โ€” Forces getopt to use the first calling format as specified in the SYNOPSIS.

๐Ÿ’€ BUGS ๐Ÿ’€

Not me exposing the flaws but okay, we keep it real here.
  • getopt(3) can parse long options with optional arguments that are given an empty optional argument โ€” but it cannot do this for short options. And this getopt(1) treats optional arguments that are empty as if they were not present. Wild, I know. ๐Ÿ™ˆ
  • The syntax for when you don't want any short option variables at all is not very intuitive. You literally have to set them explicitly to the empty string. It's giving "why is it like this" but that's just how it is. ๐Ÿ˜ญ

๐Ÿ‘‘ AUTHOR ๐Ÿ‘‘

Frodo Looijaard <frodo@frodo.looijaard.name> โ€” iconic name, iconic author, no notes. โœจ


๐Ÿ”— SEE ALSO ๐Ÿ”—

bash(1), tcsh(1), getopt(3)


๐Ÿ› REPORTING BUGS ๐Ÿ›

For bug reports, use the issue tracker at <https://github.com/util-linux/util-linux/issues>. Found a problem? Tell them, bestie, they wanna know. ๐Ÿ’–


๐Ÿ“ฆ AVAILABILITY ๐Ÿ“ฆ

The getopt command is part of the util-linux package, which can be downloaded from the Linux Kernel Archive at <https://www.kernel.org/pub/linux/utils/util-linux/>. Go get your goodies. ๐ŸŽ


And that's the whole page, bestie โ€” this one's from <b>util-linux 2.42.4</b>, dated <b>2026-09-21</b>, and it's the <b>GETOPT(1)</b> manual. Go forth and parse responsibly. ๐Ÿ’…โœจ๐Ÿฆ‹

๐Ÿ“ฆ where this page comes from
  • arch rolling: util-linux 2.42.4-1 ยท BSD-2-Clause AND BSD-3-Clause AND BSD-4-Clause-UC AND GPL-2.0-only AND GPL-2.0-or-later AND GPL-3.0-or-later AND ISC AND LGPL-2.1-or-later AND LicenseRef-PublicDomain
  • ๐Ÿ’– girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms