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

NAME

6       getopts - parse utility options
7

SYNOPSIS

9       getopts optstring name [arg...]
10

DESCRIPTION

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

OPTIONS

79       None.
80

OPERANDS

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

STDIN

114       Not used.
115

INPUT FILES

117       None.
118

ENVIRONMENT VARIABLES

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

ASYNCHRONOUS EVENTS

150       Default.
151

STDOUT

153       Not used.
154

STDERR

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

OUTPUT FILES

180       None.
181

EXTENDED DESCRIPTION

183       None.
184

EXIT STATUS

186       The following exit values shall be returned:
187
188        0     An option, specified or unspecified by optstring, was found.
189
190       >0     The end of options was encountered or an error occurred.
191
192

CONSEQUENCES OF ERRORS

194       Default.
195
196       The following sections are informative.
197

APPLICATION USAGE

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

EXAMPLES

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

RATIONALE

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

FUTURE DIRECTIONS

285       None.
286

SEE ALSO

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