Linux "getopt" Command Line Options and Examples
Parse command-line options
getopt is used to break up (parse) options in command lines for easy parsing by shell procedures, and to check for legal options. It uses the GNU getopt(3) routines to do this.
getopt optstring parameters
getopt [options] [--] optstring parameters
getopt [options] -o|--options optstring [options] [--] parameters
Command Line Options:
Allow long options to start with a single '-'.
getopt -a ...
Display help text and exit. No other output is generated.
getopt -h ...
The long (multi-character) options to be recognized. More than one option name may be specified at once, by separating thenames with commas. This option may be given more than once, the longopts are cumulative. Each long option name in longoptsmay be followed by one colon to indicate it has a required argument, and by two colons to indicate it has an optional argu‐ment.
getopt -l ...
The name that will be used by the getopt(3) routines when it reports errors. Note that errors of getopt(1) are still reportedas coming from getopt.
getopt -n ...
The short (one-character) options to be recognized. If this option is not found, the first parameter of getopt that does notstart with a '-' (and is not an option argument) is used as the short options string. Each short option character in short‐opts may be followed by one colon to indicate it has a required argument, and by two colons to indicate it has an optionalargument. 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).
getopt -o ...
Disable error reporting by getopt(3).
getopt -q ...
Do not generate normal output. Errors are still reported by getopt(3), unless you also use -q.
getopt -Q ...
Set quoting conventions to those of shell. If the -s option is not given, the BASH conventions are used. Valid arguments arecurrently 'sh' 'bash', 'csh', and 'tcsh'.
getopt -s ...
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 GETOPT_COMPATIBLE is set, will return '--'and error status 0.
getopt -T ...
Do not quote the output. Note that whitespace and special (shell-dependent) characters can cause havoc in this mode (likethey do with other getopt(1) implementations).
getopt -u ...
Display version information and exit. No other output is generated.PARSINGThis 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 is generated. These parameters were typically the parameters a shell function was called with.Care must be taken that each parameter the shell function was called with corresponds to exactly one parameter in the parameter listof getopt (see the EXAMPLES). All parsing is done by the GNU getopt(3) routines.The parameters are parsed from left to right. Each parameter is classified as a short option, a long option, an argument to anoption, or a non-option parameter.A simple short option is a '-' followed by a short option character. If the option has a required argument, it may be writtendirectly after the option character or as the next parameter (i.e. separated by whitespace on the command line). If the option hasan optional argument, it must be written directly after the option character if present.It is possible to specify several short options after one '-', as long as all (except possibly the last) do not have required oroptional arguments.A long option normally begins with '--' followed by the long option name. If the option has a required argument, it may be writtendirectly 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 (if youadd the '=' but nothing behind it, it is interpreted as if no argument was present; this is a slight bug, see the BUGS). Longoptions may be abbreviated, as long as the abbreviation is not ambiguous.Each parameter not starting with a '-', and not a required argument of a previous option, is a non-option parameter. Each parameterafter a '--' parameter is always interpreted as a non-option parameter. If the environment variable POSIXLY_CORRECT is set, or ifthe short option string started with a '+', all remaining parameters are interpreted as non-option parameters as soon as the firstnon-option parameter is found.OUTPUTOutput is generated for each element described in the previous section. Output is done in the same order as the elements are speci‐fied in the input, except for non-option parameters. Output can be done in compatible (unquoted) mode, or in such way that white‐space and other special characters within arguments and non-option parameters are preserved (see QUOTING). When the output is pro‐cessed in the shell script, it will seem to be composed of distinct elements that can be processed one by one (by using the shiftcommand in most shell languages). This is imperfect in unquoted mode, as elements can be split at unexpected places if they containwhitespace or special characters.If there are problems parsing the parameters, for example because a required argument is not found or an option is not recognized, anerror will be 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 nextparameter will be the argument. If the option takes an optional argument, but none was found, the next parameter will be generatedbut be empty in quoting mode, but no second parameter will be generated in unquoted (compatible) mode. Note that many othergetopt(1) implementations do not support optional arguments.If several short options were specified after a single '-', each 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 is done regardless whether the option wasabbreviated or specified with a single '-' in the input. Arguments are handled as with short options.Normally, no non-option parameters output is generated until all options and their arguments have been generated. Then '--' is gen‐erated as a single parameter, and after it the non-option parameters in the order they were found, each as a separate parameter.Only if the first character of the short options string was a '-', non-option parameter output is generated at the place they arefound in the input (this is not supported if the first format of the SYNOPSIS is used; in that case all preceding occurrences of '-'and '+' are ignored).QUOTINGIn compatible mode, whitespace or 'special' characters in arguments or non-option parameters are not handled correctly. As the out‐put is fed to the shell script, the script does not know how it is supposed to break the output into separate parameters. To circum‐vent this problem, this implementation offers quoting. The idea is that output is generated with quotes around each parameter. Whenthis output is once again fed to the shell (usually by a shell eval command), it is split correctly into separate parameters.Quoting is not enabled if the environment variable GETOPT_COMPATIBLE is set, if the first form of the SYNOPSIS is used, or if theoption '-u' is found.Different shells use different quoting conventions. You can use the '-s' option to select the shell you are using. The followingshells are currently supported: 'sh', 'bash', 'csh' and 'tcsh'. Actually, only two 'flavors' are distinguished: sh-like quoting con‐ventions and csh-like quoting conventions. Chances are that if you use another shell script language, one of these flavors can stillbe used.SCANNING MODESThe first character of the short options string may be a '-' or a '+' to indicate a special scanning mode. If the first calling formin the SYNOPSIS is used they are ignored; the environment variable POSIXLY_CORRECT is still examined, though.If the first character is '+', or if the environment variable POSIXLY_CORRECT is set, parsing stops as soon as the first non-optionparameter (i.e. a parameter that does not start with a '-') is found that is not an option argument. The remaining parameters areall interpreted as non-option parameters.If the first character is a '-', non-option parameters are outputted at the place where they are found; in normal operation, they areall collected at the end of output after a '--' parameter has been generated. Note that this '--' parameter is still generated, butit will always be the last parameter in this mode.COMPATIBILITYThis version of getopt(1) is written to be as compatible as possible to other versions. Usually you can just replace them with thisversion without any modifications, and with some advantages.If the first character of the first parameter of getopt is not a '-', getopt goes into compatibility mode. It will interpret itsfirst parameter as the string of short options, and all other arguments will be parsed. It will still do parameter shuffling (i.e.all non-option parameters are output at the end), unless the environment variable POSIXLY_CORRECT is set.The environment variable GETOPT_COMPATIBLE forces getopt into compatibility mode. Setting both this environment variable andPOSIXLY_CORRECT offers 100% compatibility for 'difficult' programs. Usually, though, neither is needed.In compatibility mode, leading '-' and '+' characters in the short options string are ignored.RETURN CODESgetopt returns error code 0 for successful parsing, 1 if getopt(3) returns errors, 2 if it does not understand its own parameters, 3if an internal error occurs like out-of-memory, and 4 if it is called with -T.EXAMPLESExample scripts for (ba)sh and (t)csh are provided with the getopt(1) distribution, and are optionally installed in/usr/share/getopt/ or /usr/share/doc/ in the util-linux subdirectory.ENVIRONMENTPOSIXLY_CORRECTThis environment variable is examined by the getopt(3) routines. If it is set, parsing stops as soon as a parameter is foundthat is not an option or an option argument. All remaining parameters are also interpreted as non-option parameters, regard‐less whether they start with a '-'.GETOPT_COMPATIBLEForces getopt to use the first calling format as specified in the SYNOPSIS.BUGSgetopt(3) can parse long options with optional arguments that are given an empty optional argument (but cannot do this for shortoptions). This getopt(1) treats optional arguments that are empty as if they were not present.The syntax if you do not want any short option variables at all is not very intuitive (you have to set them explicitly to the emptystring).AUTHORFrodo Looijaard ⟨email@example.com⟩
getopt -V ...