1GETOPTS(1P)                POSIX Programmer's Manual               GETOPTS(1P)
2
3
4

PROLOG

6       This  manual  page is part of the POSIX Programmer's Manual.  The Linux
7       implementation of this interface may differ (consult the  corresponding
8       Linux  manual page for details of Linux behavior), or the interface may
9       not be implemented on Linux.
10

NAME

12       getopts - parse utility options
13

SYNOPSIS

15       getopts optstring name [arg...]
16

DESCRIPTION

18       The getopts utility shall retrieve options and option-arguments from  a
19       list of parameters. It shall support the Utility Syntax Guidelines 3 to
20       10,  inclusive,  described  in   the   Base   Definitions   volume   of
21       IEEE Std 1003.1-2001, Section 12.2, Utility Syntax Guidelines.
22
23       Each  time  it is invoked, the getopts utility shall place the value of
24       the next option in the shell variable specified by the name operand and
25       the  index  of  the next argument to be processed in the shell variable
26       OPTIND.  Whenever the shell is invoked, OPTIND shall be initialized  to
27       1.
28
29       When  the option requires an option-argument, the getopts utility shall
30       place it in the shell variable OPTARG.  If no option was found,  or  if
31       the  option  that  was  found  does not have an option-argument, OPTARG
32       shall be unset.
33
34       If an option character not contained in the optstring operand is  found
35       where  an option character is expected, the shell variable specified by
36       name shall be set to the question-mark ( '?' ) character. In this case,
37       if the first character in optstring is a colon ( ':' ), the shell vari‐
38       able OPTARG shall be set to the option character found, but  no  output
39       shall  be  written  to  standard  error;  otherwise, the shell variable
40       OPTARG shall be unset and a diagnostic  message  shall  be  written  to
41       standard  error.  This  condition  shall  be  considered to be an error
42       detected in the way arguments were presented to the  invoking  applica‐
43       tion, but shall not be an error in getopts processing.
44
45       If an option-argument is missing:
46
47        * If  the  first character of optstring is a colon, the shell variable
48          specified by name shall be set to the colon character and the  shell
49          variable OPTARG shall be set to the option character found.
50
51        * Otherwise,  the shell variable specified by name shall be set to the
52          question-mark character, the shell variable OPTARG shall  be  unset,
53          and  a  diagnostic  message shall be written to standard error. This
54          condition shall be considered to be an error  detected  in  the  way
55          arguments  were presented to the invoking application, but shall not
56          be an error in getopts processing; a  diagnostic  message  shall  be
57          written as stated, but the exit status shall be zero.
58
59       When  the end of options is encountered, the getopts utility shall exit
60       with a return value greater than zero; the shell variable OPTIND  shall
61       be  set  to the index of the first non-option-argument, where the first
62       "--" argument is considered to be an option-argument if  there  are  no
63       other non-option-arguments appearing before it, or the value "$#" +1 if
64       there are no non-option-arguments; the name variable shall  be  set  to
65       the  question-mark  character.  Any of the following shall identify the
66       end of options: the special option "--", finding an argument that  does
67       not begin with a '-', or encountering an error.
68
69       The  shell  variables OPTIND and OPTARG shall be local to the caller of
70       getopts and shall not be exported by default.
71
72       The shell variable specified by the name operand, OPTIND ,  and  OPTARG
73       shall  affect the current shell execution environment; see Shell Execu‐
74       tion Environment .
75
76       If the application sets OPTIND to the value 1, a new set of  parameters
77       can  be  used: either the current positional parameters or new arg val‐
78       ues. Any other attempt to invoke getopts multiple  times  in  a  single
79       shell  execution  environment with parameters (positional parameters or
80       arg operands) that are not the same in  all  invocations,  or  with  an
81       OPTIND  value modified to be a value other than 1, produces unspecified
82       results.
83

OPTIONS

85       None.
86

OPERANDS

88       The following operands shall be supported:
89
90       optstring
91              A string containing the  option  characters  recognized  by  the
92              utility invoking getopts. If a character is followed by a colon,
93              the option shall be expected to have an argument,  which  should
94              be  supplied as a separate argument. Applications should specify
95              an option character and its option-argument  as  separate  argu‐
96              ments,  but  getopts shall interpret the characters following an
97              option character requiring arguments as an argument  whether  or
98              not  this  is done. An explicit null option-argument need not be
99              recognized if it is not supplied as  a  separate  argument  when
100              getopts  is  invoked. (See also the getopt() function defined in
101              the System  Interfaces  volume  of  IEEE Std 1003.1-2001.)   The
102              characters  question-mark  and colon shall not be used as option
103              characters by an application. The use of other option characters
104              that  are  not alphanumeric produces unspecified results. If the
105              option-argument is not supplied as a separate argument from  the
106              option  character,  the value in OPTARG shall be stripped of the
107              option character and the '-' . The first character in  optstring
108              determines  how  getopts  behaves  if an option character is not
109              known or an option-argument is missing.
110
111       name   The name of a shell variable that shall be set  by  the  getopts
112              utility to the option character that was found.
113
114
115       The getopts utility by default shall parse positional parameters passed
116       to the invoking shell procedure. If  args  are  given,  they  shall  be
117       parsed instead of the positional parameters.
118

STDIN

120       Not used.
121

INPUT FILES

123       None.
124

ENVIRONMENT VARIABLES

126       The  following  environment  variables  shall  affect  the execution of
127       getopts:
128
129       LANG   Provide a default value for the  internationalization  variables
130              that  are  unset  or  null.  (See the Base Definitions volume of
131              IEEE Std 1003.1-2001, Section  8.2,  Internationalization  Vari‐
132              ables  for the precedence of internationalization variables used
133              to determine the values of locale categories.)
134
135       LC_ALL If set to a non-empty string value, override the values  of  all
136              the other internationalization variables.
137
138       LC_CTYPE
139              Determine  the  locale  for  the  interpretation of sequences of
140              bytes of text data as characters (for  example,  single-byte  as
141              opposed to multi-byte characters in arguments and input files).
142
143       LC_MESSAGES
144              Determine  the  locale  that should be used to affect the format
145              and contents of diagnostic messages written to standard error.
146
147       NLSPATH
148              Determine the location of message catalogs for the processing of
149              LC_MESSAGES .
150
151       OPTIND This  variable shall be used by the getopts utility as the index
152              of the next argument to be processed.
153
154

ASYNCHRONOUS EVENTS

156       Default.
157

STDOUT

159       Not used.
160

STDERR

162       Whenever an error is detected and the first character in the  optstring
163       operand  is  not a colon ( ':' ), a diagnostic message shall be written
164       to standard error with the following information in an unspecified for‐
165       mat:
166
167        * The  invoking  program  name shall be identified in the message. The
168          invoking program name shall be the value of the shell special param‐
169          eter  0 (see Special Parameters ) at the time the getopts utility is
170          invoked. A name equivalent to:
171
172
173          basename "$0"
174
175       may be used.
176
177        * If an option is found that was  not  specified  in  optstring,  this
178          error  is identified and the invalid option character shall be iden‐
179          tified in the message.
180
181        * If an option requiring an option-argument is found, but  an  option-
182          argument  is  not  found,  this  error  shall  be identified and the
183          invalid option character shall be identified in the message.
184

OUTPUT FILES

186       None.
187

EXTENDED DESCRIPTION

189       None.
190

EXIT STATUS

192       The following exit values shall be returned:
193
194        0     An option, specified or unspecified by optstring, was found.
195
196       >0     The end of options was encountered or an error occurred.
197
198

CONSEQUENCES OF ERRORS

200       Default.
201
202       The following sections are informative.
203

APPLICATION USAGE

205       Since getopts affects the current shell execution  environment,  it  is
206       generally  provided  as  a shell regular built-in. If it is called in a
207       subshell or separate utility execution environment, such as one of  the
208       following:
209
210
211              (getopts abc value "$@")
212              nohup getopts ...
213              find . -exec getopts ... \;
214
215       it does not affect the shell variables in the caller's environment.
216
217       Note  that  shell  functions  share  OPTIND with the calling shell even
218       though the positional parameters are changed. If the calling shell  and
219       any  of  its functions uses getopts to parse arguments, the results are
220       unspecified.
221

EXAMPLES

223       The following example script parses and displays its arguments:
224
225
226              aflag=
227              bflag=
228              while getopts ab: name
229              do
230                  case $name in
231                  a)    aflag=1;;
232                  b)    bflag=1
233                        bval="$OPTARG";;
234                  ?)    printf "Usage: %s: [-a] [-b value] args\n" $0
235                        exit 2;;
236                  esac
237              done
238              if [ ! -z "$aflag" ]; then
239                  printf "Option -a specified\n"
240              fi
241              if [ ! -z "$bflag" ]; then
242                  printf 'Option -b "%s" specified\n' "$bval"
243              fi
244              shift $(($OPTIND - 1))
245              printf "Remaining arguments are: %s\n" "$*"
246

RATIONALE

248       The getopts utility was chosen in preference to  the  System  V  getopt
249       utility because getopts handles option-arguments containing <blank>s.
250
251       The  OPTARG variable is not mentioned in the ENVIRONMENT VARIABLES sec‐
252       tion because it does not affect the execution of getopts; it is one  of
253       the few "output-only" variables used by the standard utilities.
254
255       The  colon  is  not  allowed as an option character because that is not
256       historical behavior, and it violates the Utility Syntax Guidelines. The
257       colon  is  now  specified  to behave as in the KornShell version of the
258       getopts utility; when used as the first character in the optstring  op‐
259       erand,  it disables diagnostics concerning missing option-arguments and
260       unexpected option characters. This replaces the use of the OPTERR vari‐
261       able that was specified in an early proposal.
262
263       The  formats of the diagnostic messages produced by the getopts utility
264       and the getopt() function are not fully specified  because  implementa‐
265       tions  with  superior  (``friendlier")  formats objected to the formats
266       used by some historical implementations. The standard  developers  con‐
267       sidered  it important that the information in the messages used be uni‐
268       form between getopts and getopt(). Exact duplication  of  the  messages
269       might  not  be  possible, particularly if a utility is built on another
270       system that has a different getopt() function, but  the  messages  must
271       have  specific  information  included so that the program name, invalid
272       option character, and type of error can be distinguished by a user.
273
274       Only a rare application program intercepts  a  getopts  standard  error
275       message  and  wants to parse it. Therefore, implementations are free to
276       choose the most usable messages they can devise. The following  formats
277       are used by many historical implementations:
278
279
280              "%s: illegal option -- %c\n", <program name>, <option character>
281
282
283              "%s: option requires an argument -- %c\n", <program name>, \
284                  <option character>
285
286       Historical  shells  with  built-in versions of getopt() or getopts have
287       used different formats, frequently not even indicating the option char‐
288       acter found in error.
289

FUTURE DIRECTIONS

291       None.
292

SEE ALSO

294       Special     Parameters,     the    System    Interfaces    volume    of
295       IEEE Std 1003.1-2001, getopt()
296
298       Portions of this text are reprinted and reproduced in  electronic  form
299       from IEEE Std 1003.1, 2003 Edition, Standard for Information Technology
300       -- Portable Operating System Interface (POSIX),  The  Open  Group  Base
301       Specifications  Issue  6,  Copyright  (C) 2001-2003 by the Institute of
302       Electrical and Electronics Engineers, Inc and The Open  Group.  In  the
303       event of any discrepancy between this version and the original IEEE and
304       The Open Group Standard, the original IEEE and The Open Group  Standard
305       is  the  referee document. The original Standard can be obtained online
306       at http://www.opengroup.org/unix/online.html .
307
308
309
310IEEE/The Open Group                  2003                          GETOPTS(1P)
Impressum