1GETOPT(1)                        User Commands                       GETOPT(1)
2
3
4

NAME

6       getopt - parse command options (enhanced)
7

SYNOPSIS

9       getopt optstring parameters
10
11       getopt [options] [--] optstring parameters
12
13       getopt [options] -o|--options optstring [options] [--] parameters
14

DESCRIPTION

16       getopt is used to break up (parse) options in command lines for easy
17       parsing by shell procedures, and to check for valid options. It uses
18       the GNU getopt(3) routines to do this.
19
20       The parameters getopt is called with can be divided into two parts:
21       options which modify the way getopt will do the parsing (the options
22       and the optstring in the SYNOPSIS), and the parameters which are to be
23       parsed (parameters in the SYNOPSIS). The second part will start at the
24       first non-option parameter that is not an option argument, or after the
25       first occurrence of '--'. If no '-o' or '--options' option is found in
26       the first part, the first parameter of the second part is used as the
27       short options string.
28
29       If the environment variable GETOPT_COMPATIBLE is set, or if the first
30       parameter is not an option (does not start with a '-', the first format
31       in the SYNOPSIS), getopt will generate output that is compatible with
32       that of other versions of getopt(1). It will still do parameter
33       shuffling and recognize optional arguments (see the COMPATIBILITY
34       section for more information).
35
36       Traditional implementations of getopt(1) are unable to cope with
37       whitespace and other (shell-specific) special characters in arguments
38       and non-option parameters. To solve this problem, this implementation
39       can generate quoted output which must once again be interpreted by the
40       shell (usually by using the eval command). This has the effect of
41       preserving those characters, but you must call getopt in a way that is
42       no longer compatible with other versions (the second or third format in
43       the SYNOPSIS). To determine whether this enhanced version of getopt(1)
44       is installed, a special test option (-T) can be used.
45

OPTIONS

47       -a, --alternative
48           Allow long options to start with a single '-'.
49
50       -l, --longoptions longopts
51           The long (multi-character) options to be recognized. More than one
52           option name may be specified at once, by separating the names with
53           commas. This option may be given more than once, the longopts are
54           cumulative. Each long option name in longopts may be followed by
55           one colon to indicate it has a required argument, and by two colons
56           to indicate it has an optional argument.
57
58       -n, --name progname
59           The name that will be used by the getopt(3) routines when it
60           reports errors. Note that errors of getopt(1) are still reported as
61           coming from getopt.
62
63       -o, --options shortopts
64           The short (one-character) options to be recognized. If this option
65           is not found, the first parameter of getopt that does not start
66           with a '-' (and is not an option argument) is used as the short
67           options string. Each short option character in shortopts may be
68           followed by one colon to indicate it has a required argument, and
69           by two colons to indicate it has an optional argument. The first
70           character of shortopts may be '+' or '-' to influence the way
71           options are parsed and output is generated (see the SCANNING MODES
72           section for details).
73
74       -q, --quiet
75           Disable error reporting by getopt(3).
76
77       -Q, --quiet-output
78           Do not generate normal output. Errors are still reported by
79           getopt(3), unless you also use -q.
80
81       -s, --shell shell
82           Set quoting conventions to those of shell. If the -s option is not
83           given, the BASH conventions are used. Valid arguments are currently
84           'sh', 'bash', 'csh', and 'tcsh'.
85
86       -T, --test
87           Test if your getopt(1) is this enhanced version or an old version.
88           This generates no output, and sets the error status to 4. Other
89           implementations of getopt(1), and this version if the environment
90           variable GETOPT_COMPATIBLE is set, will return '--' and error
91           status 0.
92
93       -u, --unquoted
94           Do not quote the output. Note that whitespace and special
95           (shell-dependent) characters can cause havoc in this mode (like
96           they do with other getopt(1) implementations).
97
98       -h, --help
99           Display help text and exit.
100
101       -V, --version
102           Print version and exit.
103

PARSING

105       This section specifies the format of the second part of the parameters
106       of getopt (the parameters in the SYNOPSIS). The next section (OUTPUT)
107       describes the output that is generated. These parameters were typically
108       the parameters a shell function was called with. Care must be taken
109       that each parameter the shell function was called with corresponds to
110       exactly one parameter in the parameter list of getopt (see the
111       EXAMPLES). All parsing is done by the GNU getopt(3) routines.
112
113       The parameters are parsed from left to right. Each parameter is
114       classified as a short option, a long option, an argument to an option,
115       or a non-option parameter.
116
117       A simple short option is a '-' followed by a short option character. If
118       the option has a required argument, it may be written directly after
119       the option character or as the next parameter (i.e., separated by
120       whitespace on the command line). If the option has an optional
121       argument, it must be written directly after the option character if
122       present.
123
124       It is possible to specify several short options after one '-', as long
125       as all (except possibly the last) do not have required or optional
126       arguments.
127
128       A long option normally begins with '--' followed by the long option
129       name. If the option has a required argument, it may be written directly
130       after the long option name, separated by '=', or as the next argument
131       (i.e., separated by whitespace on the command line). If the option has
132       an optional argument, it must be written directly after the long option
133       name, separated by '=', if present (if you add the '=' but nothing
134       behind it, it is interpreted as if no argument was present; this is a
135       slight bug, see the BUGS). Long options may be abbreviated, as long as
136       the abbreviation is not ambiguous.
137
138       Each parameter not starting with a '-', and not a required argument of
139       a previous option, is a non-option parameter. Each parameter after a
140       '--' parameter is always interpreted as a non-option parameter. If the
141       environment variable POSIXLY_CORRECT is set, or if the short option
142       string started with a '+', all remaining parameters are interpreted as
143       non-option parameters as soon as the first non-option parameter is
144       found.
145

OUTPUT

147       Output is generated for each element described in the previous section.
148       Output is done in the same order as the elements are specified in the
149       input, except for non-option parameters. Output can be done in
150       compatible (unquoted) mode, or in such way that whitespace and other
151       special characters within arguments and non-option parameters are
152       preserved (see QUOTING). When the output is processed in the shell
153       script, it will seem to be composed of distinct elements that can be
154       processed one by one (by using the shift command in most shell
155       languages). This is imperfect in unquoted mode, as elements can be
156       split at unexpected places if they contain whitespace or special
157       characters.
158
159       If there are problems parsing the parameters, for example because a
160       required argument is not found or an option is not recognized, an error
161       will be reported on stderr, there will be no output for the offending
162       element, and a non-zero error status is returned.
163
164       For a short option, a single '-' and the option character are generated
165       as one parameter. If the option has an argument, the next parameter
166       will be the argument. If the option takes an optional argument, but
167       none was found, the next parameter will be generated but be empty in
168       quoting mode, but no second parameter will be generated in unquoted
169       (compatible) mode. Note that many other getopt(1) implementations do
170       not support optional arguments.
171
172       If several short options were specified after a single '-', each will
173       be present in the output as a separate parameter.
174
175       For a long option, '--' and the full option name are generated as one
176       parameter. This is done regardless whether the option was abbreviated
177       or specified with a single '-' in the input. Arguments are handled as
178       with short options.
179
180       Normally, no non-option parameters output is generated until all
181       options and their arguments have been generated. Then '--' is generated
182       as a single parameter, and after it the non-option parameters in the
183       order they were found, each as a separate parameter. Only if the first
184       character of the short options string was a '-', non-option parameter
185       output is generated at the place they are found in the input (this is
186       not supported if the first format of the SYNOPSIS is used; in that case
187       all preceding occurrences of '-' and '+' are ignored).
188

QUOTING

190       In compatibility mode, whitespace or 'special' characters in arguments
191       or non-option parameters are not handled correctly. As the output is
192       fed to the shell script, the script does not know how it is supposed to
193       break the output into separate parameters. To circumvent this problem,
194       this implementation offers quoting. The idea is that output is
195       generated with quotes around each parameter. When this output is once
196       again fed to the shell (usually by a shell eval command), it is split
197       correctly into separate parameters.
198
199       Quoting is not enabled if the environment variable GETOPT_COMPATIBLE is
200       set, if the first form of the SYNOPSIS is used, or if the option '-u'
201       is found.
202
203       Different shells use different quoting conventions. You can use the
204       '-s' option to select the shell you are using. The following shells are
205       currently supported: 'sh', 'bash', 'csh' and 'tcsh'. Actually, only two
206       'flavors' are distinguished: sh-like quoting conventions and csh-like
207       quoting conventions. Chances are that if you use another shell script
208       language, one of these flavors can still be used.
209

SCANNING MODES

211       The first character of the short options string may be a '-' or a '+'
212       to indicate a special scanning mode. If the first calling form in the
213       SYNOPSIS is used they are ignored; the environment variable
214       POSIXLY_CORRECT is still examined, though.
215
216       If the first character is '+', or if the environment variable
217       POSIXLY_CORRECT is set, parsing stops as soon as the first non-option
218       parameter (i.e., a parameter that does not start with a '-') is found
219       that is not an option argument. The remaining parameters are all
220       interpreted as non-option parameters.
221
222       If the first character is a '-', non-option parameters are outputted at
223       the place where they are found; in normal operation, they are all
224       collected at the end of output after a '--' parameter has been
225       generated. Note that this '--' parameter is still generated, but it
226       will always be the last parameter in this mode.
227

COMPATIBILITY

229       This version of getopt(1) is written to be as compatible as possible to
230       other versions. Usually you can just replace them with this version
231       without any modifications, and with some advantages.
232
233       If the first character of the first parameter of getopt is not a '-',
234       getopt goes into compatibility mode. It will interpret its first
235       parameter as the string of short options, and all other arguments will
236       be parsed. It will still do parameter shuffling (i.e., all non-option
237       parameters are output at the end), unless the environment variable
238       POSIXLY_CORRECT is set, in which case, getopt will prepend a '+' before
239       short options automatically.
240
241       The environment variable GETOPT_COMPATIBLE forces getopt into
242       compatibility mode. Setting both this environment variable and
243       POSIXLY_CORRECT offers 100% compatibility for 'difficult' programs.
244       Usually, though, neither is needed.
245
246       In compatibility mode, leading '-' and '+' characters in the short
247       options string are ignored.
248

RETURN CODES

250       getopt returns error code 0 for successful parsing, 1 if getopt(3)
251       returns errors, 2 if it does not understand its own parameters, 3 if an
252       internal error occurs like out-of-memory, and 4 if it is called with
253       -T.
254

EXAMPLES

256       Example scripts for (ba)sh and (t)csh are provided with the getopt(1)
257       distribution, and are installed in /usr/share/doc/util-linux directory.
258

ENVIRONMENT

260       POSIXLY_CORRECT
261           This environment variable is examined by the getopt(3) routines. If
262           it is set, parsing stops as soon as a parameter is found that is
263           not an option or an option argument. All remaining parameters are
264           also interpreted as non-option parameters, regardless whether they
265           start with a '-'.
266
267       GETOPT_COMPATIBLE
268           Forces getopt to use the first calling format as specified in the
269           SYNOPSIS.
270

BUGS

272       getopt(3) can parse long options with optional arguments that are given
273       an empty optional argument (but cannot do this for short options). This
274       getopt(1) treats optional arguments that are empty as if they were not
275       present.
276
277       The syntax if you do not want any short option variables at all is not
278       very intuitive (you have to set them explicitly to the empty string).
279

AUTHOR

281       Frodo Looijaard <frodo@frodo.looijaard.name>
282

SEE ALSO

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

REPORTING BUGS

287       For bug reports, use the issue tracker at
288       https://github.com/util-linux/util-linux/issues.
289

AVAILABILITY

291       The getopt command is part of the util-linux package which can be
292       downloaded from Linux Kernel Archive
293       <https://www.kernel.org/pub/linux/utils/util-linux/>.
294
295
296
297util-linux 2.38.1                 2022-05-11                         GETOPT(1)
Impressum