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

NAME

6       getopt - parse command options (enhanced)
7

SYNOPSIS

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

DESCRIPTION

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

OPTIONS

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

PARSING

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

OUTPUT

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

QUOTING

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

SCANNING MODES

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

COMPATIBILITY

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

RETURN CODES

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

EXAMPLES

252       Example scripts for (ba)sh and (t)csh are provided with  the  getopt(1)
253       distribution,  and  are  optionally  installed in /usr/share/getopt/ or
254       /usr/share/doc/ in the util-linux subdirectory.
255

ENVIRONMENT

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

BUGS

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

AUTHOR

278       Frodo Looijaard ⟨frodo@frodo.looijaard.name⟩
279

SEE ALSO

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

AVAILABILITY

284       The  getopt  command is part of the util-linux package and is available
285       from Linux Kernel Archive ⟨https://www.kernel.org/pub/linux/utils/util-
286       linux/⟩.
287
288
289
290util-linux                       December 2014                       GETOPT(1)
Impressum