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   parse   (options   and
20       -o|--options  optstring  in the SYNOPSIS), and the parameters which are
21       to be parsed (parameters in the SYNOPSIS).  The second part will  start
22       at  the  first  non-option parameter that is not an option argument, or
23       after the first occurrence of '--'.  If no '-o' or  '--options'  option
24       is  found  in the first part, the first parameter of the second part is
25       used as the short options string.
26
27       If the environment variable GETOPT_COMPATIBLE is set, or if  its  first
28       parameter  is  not  an  option  (does not start with a '-', this is the
29       first format in the SYNOPSIS), getopt will generate output that is com‐
30       patible  with  that  of  other versions of getopt(1).  It will still do
31       parameter shuffling and recognize optional arguments (see section  COM‐
32       PATIBILITY for 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              Output a small usage guide and exit successfully.  No other out‐
50              put is generated.
51
52       -l, --longoptions longopts
53              The long (multi-character) options to be recognized.  More  than
54              one  option  name  may  be  specified at once, by separating the
55              names with commas.  This option may be given more than once, the
56              longopts  are cumulative.  Each long option name in longopts may
57              be followed by one colon to indicate it has a required argument,
58              and by two colons to indicate it has an optional argument.
59
60       -n, --name progname
61              The  name  that  will  be used by the getopt(3) routines when it
62              reports  errors.   Note  that  errors  of  getopt(1)  are  still
63              reported as coming from getopt.
64
65       -o, --options shortopts
66              The  short  (one-character)  options  to be recognized.  If this
67              option is not found, the first parameter of getopt that does not
68              start  with a '-' (and is not an option argument) is used as the
69              short options string.  Each short option character in  shortopts
70              may be followed by one colon to indicate it has a required argu‐
71              ment, and by two colons to indicate it has an optional argument.
72              The  first character of shortopts may be '+' or '-' to influence
73              the way options are parsed and output is generated (see  section
74              SCANNING MODES for details).
75
76       -q, --quiet
77              Disable error reporting by getopt(3).
78
79       -Q, --quiet-output
80              Do  not  generate  normal  output.  Errors are still reported by
81              getopt(3), unless you also use -q.
82
83       -s, --shell shell
84              Set quoting conventions to those of shell.  If no -s argument is
85              found,  the BASH conventions are used.  Valid arguments are cur‐
86              rently 'sh' 'bash', 'csh', and 'tcsh'.
87
88       -u, --unquoted
89              Do not quote the  output.   Note  that  whitespace  and  special
90              (shell-dependent)  characters can cause havoc in this mode (like
91              they do with other getopt(1) implementations).
92
93       -T, --test
94              Test if your getopt(1) is this enhanced version or an  old  ver‐
95              sion.  This generates no output, and sets the error status to 4.
96              Other implementations of getopt(1),  and  this  version  if  the
97              environment  variable GETOPT_COMPATIBLE is set, will return '--'
98              and error status 0.
99
100       -V, --version
101              Output version information and exit successfully.  No other out‐
102              put is generated.
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 typi‐
108       cally the parameters a shell function was called with.   Care  must  be
109       taken  that  each  parameter  the shell function was called with corre‐
110       sponds to exactly one parameter in the parameter list  of  getopt  (see
111       the EXAMPLES).  All parsing is done by the GNU getopt(3) routines.
112
113       The  parameters are parsed from left to right.  Each parameter is clas‐
114       sified as a short option, a long option, an argument to an option, or a
115       non-option parameter.
116
117       A  simple  short  option is a '-' followed by a short option character.
118       If the option has a required argument, it may be written directly after
119       the option character or as the next parameter (ie.  separated by white‐
120       space on the command line).  If the option has an optional argument, it
121       must be written directly after the option character if present.
122
123       It  is possible to specify several short options after one '-', as long
124       as all (except possibly the last) do  not  have  required  or  optional
125       arguments.
126
127       A  long  option  normally  begins with '--' followed by the long option
128       name.  If the option  has  a  required  argument,  it  may  be  written
129       directly  after  the long option name, separated by '=', or as the next
130       argument (i.e. separated by whitespace on the command  line).   If  the
131       option  has an optional argument, it must be written directly after the
132       long option name, separated by '=', if present (if you add the '='  but
133       nothing  behind  it,  it  is interpreted as if no argument was present;
134       this is a slight bug, see the BUGS).  Long options may be  abbreviated,
135       as long as the abbreviation is not ambiguous.
136
137       Each  parameter not starting with a '-', and not a required argument of
138       a previous option, is a non-option parameter.  Each parameter  after  a
139       '--' parameter is always interpreted as a non-option parameter.  If the
140       environment variable POSIXLY_CORRECT is set, or  if  the  short  option
141       string  started with a '+', all remaining parameters are interpreted as
142       non-option parameters as soon as  the  first  non-option  parameter  is
143       found.
144

OUTPUT

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

QUOTING

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

SCANNING MODES

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

COMPATIBILITY

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

RETURN CODES

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

EXAMPLES

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

ENVIRONMENT

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

BUGS

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

AUTHOR

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

SEE ALSO

282       getopt(3), bash(1), tcsh(1).
283

AVAILABILITY

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