1 .TH GETOPT "1" "December 2014" "util-linux" "User Commands"
3 getopt \- parse command options (enhanced)
6 .I optstring parameters
11 .I optstring parameters
24 options in command lines for easy parsing by shell procedures, and to
25 check for legal options. It uses the
32 is called with can be divided into two parts: options which modify
41 and the parameters which are to be parsed
45 The second part will start at the first non\-option parameter that is
46 not an option argument, or after the first occurrence of
52 option is found in the first part, the first parameter of the second
53 part is used as the short options string.
55 If the environment variable
57 is set, or if the first \fIparameter\fR is not an option (does not start
60 the first format in the
63 will generate output that is compatible with that of other versions of
65 It will still do parameter shuffling and recognize optional arguments
68 for more information).
70 Traditional implementations of
72 are unable to cope with whitespace and other (shell-specific)
73 special characters in arguments and non\-option parameters. To solve
74 this problem, this implementation can generate quoted output which
75 must once again be interpreted by the shell (usually by using the
77 command). This has the effect of preserving those characters, but
80 in a way that is no longer compatible with other versions (the second
81 or third format in the
83 To determine whether this enhanced version of
85 is installed, a special test option
90 .BR \-a , " \-\-alternative"
91 Allow long options to start with a single
95 Display help text and exit. No other output is generated.
97 .BR \-l , " \-\-longoptions \fIlongopts\fP"
98 The long (multi\-character) options to be recognized. More than one
99 option name may be specified at once, by separating the names with
100 commas. This option may be given more than once, the
102 are cumulative. Each long option name in
104 may be followed by one colon to indicate it has a required argument,
105 and by two colons to indicate it has an optional argument.
107 .BR \-n , " \-\-name \fIprogname\fP"
108 The name that will be used by the
110 routines when it reports errors. Note that errors of
112 are still reported as coming from getopt.
114 .BR \-o , " \-\-options \fIshortopts\fP"
115 The short (one\-character) options to be recognized. If this option
116 is not found, the first parameter of
118 that does not start with a
120 (and is not an option argument) is used as the short options string.
121 Each short option character in
123 may be followed by one colon to indicate it has a required argument,
124 and by two colons to indicate it has an optional argument. The first
125 character of shortopts may be
129 to influence the way options are parsed and output is generated (see
134 .BR \-q , " \-\-quiet"
135 Disable error reporting by getopt(3).
137 .BR \-Q , " \-\-quiet\-output"
138 Do not generate normal output. Errors are still reported by
143 .BR \-s , " \-\-shell \fIshell\fP"
144 Set quoting conventions to those of \fIshell\fR.
145 If the \fB\-s\fR option is not given, the
147 conventions are used. Valid arguments are currently
154 .BR \-T , " \-\-test"
157 is this enhanced version or an old version. This generates no
158 output, and sets the error status to 4. Other implementations of
160 and this version if the environment variable
166 .BR \-u , " \-\-unquoted"
167 Do not quote the output. Note that whitespace and special
168 (shell-dependent) characters can cause havoc in this mode (like they
173 .BR \-V , " \-\-version"
174 Display version information and exit. No other output is generated.
176 This section specifies the format of the second part of the
185 describes the output that is generated. These parameters were
186 typically the parameters a shell function was called with. Care must
187 be taken that each parameter the shell function was called with
188 corresponds to exactly one parameter in the parameter list of
192 All parsing is done by the GNU
196 The parameters are parsed from left to right. Each parameter is
197 classified as a short option, a long option, an argument to an
198 option, or a non\-option parameter.
200 A simple short option is a
202 followed by a short option character. If the option has a required
203 argument, it may be written directly after the option character or as
204 the next parameter (i.e. separated by whitespace on the command
205 line). If the option has an optional argument, it must be written
206 directly after the option character if present.
208 It is possible to specify several short options after one
210 as long as all (except possibly the last) do not have required or
213 A long option normally begins with
215 followed by the long option name. If the option has a required
216 argument, it may be written directly after the long option name,
219 or as the next argument (i.e. separated by whitespace on the command
220 line). If the option has an optional argument, it must be written
221 directly after the long option name, separated by
223 if present (if you add the
225 but nothing behind it, it is interpreted as if no argument was
226 present; this is a slight bug, see the
228 Long options may be abbreviated, as long as the abbreviation is not
231 Each parameter not starting with a
233 and not a required argument of a previous option, is a non\-option
234 parameter. Each parameter after a
236 parameter is always interpreted as a non\-option parameter. If the
239 is set, or if the short option string started with a
241 all remaining parameters are interpreted as non\-option parameters as
242 soon as the first non\-option parameter is found.
244 Output is generated for each element described in the previous
245 section. Output is done in the same order as the elements are
246 specified in the input, except for non\-option parameters. Output
250 mode, or in such way that whitespace and other special characters
251 within arguments and non\-option parameters are preserved (see
253 When the output is processed in the shell script, it will seem to be
254 composed of distinct elements that can be processed one by one (by
255 using the shift command in most shell languages). This is imperfect
256 in unquoted mode, as elements can be split at unexpected places if
257 they contain whitespace or special characters.
259 If there are problems parsing the parameters, for example because a
260 required argument is not found or an option is not recognized, an
261 error will be reported on stderr, there will be no output for the
262 offending element, and a non\-zero error status is returned.
264 For a short option, a single
266 and the option character are generated as one parameter. If the
267 option has an argument, the next parameter will be the argument. If
268 the option takes an optional argument, but none was found, the next
269 parameter will be generated but be empty in quoting mode, but no
270 second parameter will be generated in unquoted (compatible) mode.
273 implementations do not support optional arguments.
275 If several short options were specified after a single
277 each will be present in the output as a separate parameter.
281 and the full option name are generated as one parameter. This is
282 done regardless whether the option was abbreviated or specified with
285 in the input. Arguments are handled as with short options.
287 Normally, no non\-option parameters output is generated until all
288 options and their arguments have been generated. Then
290 is generated as a single parameter, and after it the non\-option
291 parameters in the order they were found, each as a separate
292 parameter. Only if the first character of the short options string
295 non\-option parameter output is generated at the place they are found
296 in the input (this is not supported if the first format of the
298 is used; in that case all preceding occurrences of
304 In compatible mode, whitespace or 'special' characters in arguments
305 or non\-option parameters are not handled correctly. As the output
306 is fed to the shell script, the script does not know how it is
307 supposed to break the output into separate parameters. To circumvent
308 this problem, this implementation offers quoting. The idea is that
309 output is generated with quotes around each parameter. When this
310 output is once again fed to the shell (usually by a shell
312 command), it is split correctly into separate parameters.
314 Quoting is not enabled if the environment variable
316 is set, if the first form of the
318 is used, or if the option
322 Different shells use different quoting conventions. You can use the
324 option to select the shell you are using. The following shells are
331 Actually, only two 'flavors' are distinguished: sh\-like quoting
332 conventions and csh\-like quoting conventions. Chances are that if
333 you use another shell script language, one of these flavors can still
336 The first character of the short options string may be a
340 to indicate a special scanning mode. If the first calling form in
343 is used they are ignored; the environment variable
345 is still examined, though.
347 If the first character is
349 or if the environment variable
351 is set, parsing stops as soon as the first non\-option parameter
352 (i.e. a parameter that does not start with a
354 is found that is not an option argument. The remaining parameters
355 are all interpreted as non\-option parameters.
357 If the first character is a
359 non\-option parameters are outputted at the place where they are
360 found; in normal operation, they are all collected at the end of
363 parameter has been generated. Note that this
365 parameter is still generated, but it will always be the last
366 parameter in this mode.
370 is written to be as compatible as possible to other versions.
371 Usually you can just replace them with this version without any
372 modifications, and with some advantages.
374 If the first character of the first parameter of getopt is not a
377 goes into compatibility mode. It will interpret its first
378 parameter as the string of short options, and all other arguments
379 will be parsed. It will still do parameter shuffling (i.e. all
380 non\-option parameters are output at the end), unless the
385 The environment variable
389 into compatibility mode. Setting both this environment variable and
391 offers 100% compatibility for 'difficult' programs. Usually, though,
394 In compatibility mode, leading
398 characters in the short options string are ignored.
403 for successful parsing,
409 if it does not understand its own parameters,
411 if an internal error occurs like out\-of\-memory, and
416 Example scripts for (ba)sh and (t)csh are provided with the
418 distribution, and are optionally installed in
419 .BR /usr/share/getopt/
422 in the util-linux subdirectory.
425 This environment variable is examined by the
427 routines. If it is set, parsing stops as soon as a parameter is
428 found that is not an option or an option argument. All remaining
429 parameters are also interpreted as non\-option parameters, regardless
430 whether they start with a
432 .IP GETOPT_COMPATIBLE
435 to use the first calling format as specified in the
439 can parse long options with optional arguments that are given an
440 empty optional argument (but can not do this for short options).
443 treats optional arguments that are empty as if they were not present.
445 The syntax if you do not want any short option variables at all is
446 not very intuitive (you have to set them explicitly to the empty
449 .MT frodo@frodo.looijaard.name
457 The getopt command is part of the util-linux package and is available from
458 .UR ftp://\:ftp.kernel.org\:/pub\:/linux\:/utils\:/util-linux/