2 .\" Sun Microsystems, Inc. gratefully acknowledges The Open Group for
3 .\" permission to reproduce portions of its copyrighted documentation.
4 .\" Original documentation from The Open Group can be obtained online at
5 .\" http://www.opengroup.org/bookstore/.
7 .\" The Institute of Electrical and Electronics Engineers and The Open
8 .\" Group, have given us permission to reprint portions of their
11 .\" In the following statement, the phrase ``this text'' refers to portions
12 .\" of the system documentation.
14 .\" Portions of this text are reprinted and reproduced in electronic form
15 .\" in the SunOS Reference Manual, from IEEE Std 1003.1, 2004 Edition,
16 .\" Standard for Information Technology -- Portable Operating System
17 .\" Interface (POSIX), The Open Group Base Specifications Issue 6,
18 .\" Copyright (C) 2001-2004 by the Institute of Electrical and Electronics
19 .\" Engineers, Inc and The Open Group. In the event of any discrepancy
20 .\" between these versions and the original IEEE and The Open Group
21 .\" Standard, the original IEEE and The Open Group Standard is the referee
22 .\" document. The original Standard can be obtained online at
23 .\" http://www.opengroup.org/unix/online.html.
25 .\" This notice shall appear on any product containing this material.
27 .\" The contents of this file are subject to the terms of the
28 .\" Common Development and Distribution License (the "License").
29 .\" You may not use this file except in compliance with the License.
31 .\" You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
32 .\" or http://www.opensolaris.org/os/licensing.
33 .\" See the License for the specific language governing permissions
34 .\" and limitations under the License.
36 .\" When distributing Covered Code, include this CDDL HEADER in each
37 .\" file and include the License file at usr/src/OPENSOLARIS.LICENSE.
38 .\" If applicable, add the following below this CDDL HEADER, with the
39 .\" fields enclosed by brackets "[]" replaced with your own identifying
40 .\" information: Portions Copyright [yyyy] [name of copyright owner]
43 .\" Copyright 1989 AT&T
44 .\" Copyright 1992, X/Open Company Limited All Rights Reserved
45 .\" Portions Copyright (c) 1982-2007 AT&T Knowledge Ventures
46 .\" Copyright (c) 2007 Sun Microsystems, Inc. All Rights Reserved
48 .TH GETOPTS 1 "Nov 2, 2007"
50 getopts \- parse utility options
54 \fB/usr/bin/getopts\fR \fIoptstring\fR \fIname\fR [\fIarg\fR...]
60 \fBgetopts\fR \fIoptstring\fR \fIname\fR [\fIargument\fR]...
66 \fBgetopts\fR \fIoptstring\fR \fIname\fR [\fIarg\fR]...
72 \fBgetopts\fR [\fB-a\fR \fIname\fR] \fIoptstring\fR \fIname\fR [\fIarg\fR]...
76 .SS "/usr/bin/getopts"
79 The \fBgetopts\fR utility can be used to retrieve options and option-arguments
80 from a list of parameters.
83 Each time it is invoked, the \fBgetopts\fR utility places the value of the next
84 option in the shell variable specified by the \fIname\fR operand and the index
85 of the next argument to be processed in the shell variable \fBOPTIND\fR.
86 Whenever the shell is invoked, \fBOPTIND\fR is initialized to \fB1\fR.
89 When the option requires an option-argument, the \fBgetopts\fR utility places
90 it in the shell variable \fBOPTARG\fR. If no option was found, or if the option
91 that was found does not have an option-argument, \fBOPTARG\fR is unset.
94 If an option character not contained in the \fIoptstring\fR operand is found
95 where an option character is expected, the shell variable specified by
96 \fIname\fR is set to the question-mark \fB(\fR \fB?\fR \fB)\fR character. In
97 this case, if the first character in \fIoptstring\fR is a colon (\fB:\fR, the
98 shell variable \fBOPTARG\fR is set to the option character found, but no output
99 is written to standard error; otherwise, the shell variable \fBOPTARG\fR is
100 unset and a diagnostic message is written to standard error. This condition is
101 considered to be an error detected in the way arguments were presented to the
102 invoking application, but is not an error in \fBgetopts\fR processing.
105 If an option-argument is missing:
110 If the first character of \fIoptstring\fR is a colon, the shell variable
111 specified by \fIname\fR is set to the colon character and the shell variable
112 \fBOPTARG\fR is set to the option character found.
118 Otherwise, the shell variable specified by \fIname\fR is set to the
119 question-mark character (\fB?\fR), the shell variable \fBOPTARG\fR is unset,
120 and a diagnostic message is written to standard error. This condition is
121 considered to be an error detected in the way arguments were presented to the
122 invoking application, but is not an error in \fBgetopts\fR processing; a
123 diagnostic message is written as stated, but the exit status is zero.
127 When the end of options is encountered, the \fBgetopts\fR utility exits with a
128 return value greater than zero; the shell variable \fBOPTIND\fR is set to the
129 index of the first non-option-argument, where the first \fB\(mi\|\(mi\fR
130 argument is considered to be an option-argument if there are no other
131 non-option-arguments appearing before it, or the value \fB$#\fR + 1 if there
132 are no non-option-arguments; the \fIname\fR variable is set to the
133 question-mark character. Any of the following identifies the end of options:
134 the special option \fB\(mi\|\(mi\fR, finding an argument that does not begin
135 with a \fB\(mi\fR, or encountering an error.
138 The shell variables \fBOPTIND\fR and \fBOPTARG\fR are local to the caller of
139 \fBgetopts\fR and are not exported by default.
142 The shell variable specified by the \fIname\fR operand, \fBOPTIND\fR and
143 \fBOPTARG\fR affect the current shell execution environment.
146 If the application sets \fBOPTIND\fR to the value 1, a new set of parameters
147 can be used: either the current positional parameters or new \fIarg\fR values.
148 Any other attempt to invoke \fBgetopts\fR multiple times in a single shell
149 execution environment with parameters (positional parameters or \fIarg\fR
150 operands) that are not the same in all invocations, or with an \fBOPTIND\fR
151 value modified to be a value other than 1, produces unspecified results.
155 \fBgetopts\fR is a built-in Bourne shell command used to parse positional
156 parameters and to check for valid options. See \fBsh\fR(1). It supports all
157 applicable rules of the command syntax standard (see Rules 3-10,
158 \fBIntro\fR(1)). It should be used in place of the \fBgetopt\fR command.
161 \fIoptstring\fR must contain the option letters the command using \fBgetopts\fR
162 recognizes. If a letter is followed by a colon, the option is expected to have
163 an argument, or group of arguments, which must be separated from it by white
167 Each time it is invoked, \fBgetopts\fR places the next option in the shell
168 variable \fIname\fR and the index of the next argument to be processed in the
169 shell variable \fBOPTIND\fR. Whenever the shell or a shell script is invoked,
170 \fBOPTIND\fR is initialized to \fB1\fR.
173 When an option requires an option-argument, \fBgetopts\fR places it in the
174 shell variable \fBOPTARG\fR.
177 If an illegal option is encountered, \fB?\fR is placed in \fIname\fR.
180 When the end of options is encountered, \fBgetopts\fR exits with a non-zero
181 exit status. The special option \fB-\fR can be used to delimit the end of the
185 By default, \fBgetopts\fR parses the positional parameters. If extra arguments
186 (\fIargument\fR .\|.\|.) are specified on the \fBgetopts\fR command line,
187 \fBgetopts\fR parses them instead.
190 \fB/usr/lib/getoptcvt\fR reads the shell script in \fIfilename\fR, converts it
191 to use \fBgetopts\fR instead of \fBgetopt\fR, and writes the results on the
195 So that all new commands adhere to the command syntax standard described in
196 \fBIntro\fR(1), they should use \fBgetopts\fR or \fBgetopt\fR to parse
197 positional parameters and check for options that are valid for that command.
200 \fBgetopts\fR prints an error message on the standard error when it encounters
201 an option letter not included in \fIoptstring\fR.
204 Although the following command syntax rule (see \fBIntro\fR(1)) relaxations are
205 permitted under the current implementation, they should not be used because
206 they can not be supported in future releases of the system. As in the EXAMPLES
207 section below, \fB-a\fR and \fB-b\fR are options, and the option \fB-o\fR
208 requires an option-argument.
211 The following example violates Rule 5: options with option-arguments must not
212 be grouped with other options:
216 example% \fBcmd -aboxxx filename\fR
223 The following example violates Rule 6: there must be white space after an
224 option that takes an option-argument:
228 example% \fBcmd -ab oxxx filename\fR
235 Changing the value of the shell variable \fBOPTIND\fR or parsing different sets
236 of arguments can lead to unexpected results.
240 Checks \fIarg\fR for legal options. If \fIarg\fR is omitted, the positional
241 parameters are used. An option argument begins with a \fB+\fR or a
242 \fB\(mi\fR\&. An option not beginning with \fB+\fR or \fB\(mi\fR or the
243 argument \fB-\fR ends the options. \fIoptstring\fR contains the letters that
244 \fBgetopts\fR recognizes. If a letter is followed by a \fB:\fR, that option is
245 expected to have an argument. The options can be separated from the argument by
249 \fBgetopts\fR places the next option letter it finds inside variable \fIname\fR
250 each time it is invoked with a \fB+\fR prepended when \fIarg\fR begins with a
251 \fB+\fR. The index of the next \fIarg\fR is stored in \fBOPTIND\fR. The option
252 argument, if any, gets stored in \fBOPTARG\fR.
255 A leading \fB:\fR in \fIoptstring\fR causes \fBgetopts\fR to store the letter
256 of an invalid option in \fBOPTARG\fR, and to set \fIname\fR to \fB?\fR for an
257 unknown option and to \fB:\fR when a required option is missing. Otherwise,
258 \fBgetopts\fR prints an error message. The exit status is \fBnon-zero\fR when
259 there are no more options.
262 \fBgetopts\fR supports both traditional single-character short options and long
263 options defined by Sun's Command Line Interface Paradigm (\fBCLIP\fR).
266 Each long option is an alias for a short option and is specified in parentheses
267 following its equivalent short option. For example, you can specify the long
268 option \fBfile\fR as an alias for the short option \fBf\fR using the following
273 getopts "f(file)" opt
280 Precede long options on the command line with \fB--\fR or \fB++\fR. In the
281 example above, \fB--file\fR on the command line would be the equivalent of
282 \fB-f\fR, and \fB++file\fR on the command line would be the equivalent of
286 Each short option can have multiple long option equivalents, although this is
287 in violation of the CLIP specification and should be used with caution. You
288 must enclose each long option equivalent parentheses, as follows:
292 getopts "f:(file)(input-file)o:(output-file)"
299 In the above example, both \fB--file\fR and \fB--input-file\fR are the
300 equivalent of \fB-f\fR, and \fB--output-file\fR is the equivalent of \fB-o\fR.
303 The variable name is always set to a short option. When a long option is
304 specified on the command line, name is set to the short-option equivalent.
307 For a further discussion of the Korn shell's \fBgetopts\fR built-in command,
308 see the previous discussion in the Bourne shell (\fBsh\fR) section of this
313 The \fBgetopts\fR utility can be used to retrieve options and arguments from a
314 list of arguments specified by \fIarg\fRs or the positional parameters if
315 \fIarg\fR is omitted. It can also generate usage messages and a manual page for
316 the command based on the information in \fIoptstring\fR.
319 Each time it is invoked, the \fBgetopts\fR utility places the value of the next
320 option in the shell variable specified by the \fIname\fR operand and the index
321 of the next argument to be processed in the shell variable \fBOPTIND\fR. When
322 the shell is invoked \fBOPTIND\fR is initialized to \fB1\fR. When an option
323 requires or permits an option argument, \fBgetopts\fR places the option
324 argument in the shell variable \fBOPTARG\fR. Otherwise \fBOPTARG\fR is set to
325 \fB1\fR when the option is set and \fB0\fR when the option is \fBunset\fR.
328 The \fIoptstring\fR string consists of alphanumeric characters, the special
329 characters \fB+\fR, \fB-\fR, \fB?\fR, \fB:\fR, and SPACE or character groups
330 enclosed in \fB[...]\fR. Character groups can be nested in \fB{...}\fR. Outside
331 of a \fB[...]\fR group, a single NEWLINE followed by zero or more blanks is
332 ignored. One or more blank lines separate the options from the command argument
336 Each \fB[...]\fR group consists of an optional label, optional attributes
337 separated by \fB:\fR, and an optional description string following \fB?\fR. The
338 characters from the \fB?\fR to the end of the next \fB]\fR are ignored for
339 option parsing and short usage messages. They are used for generating verbose
340 help or man pages. The \fB:\fR character can not appear in the label. The
341 \fB?\fR character must be specified as \fB??\fR in the label and the \fB]\fR
342 character must be specified as \fB]]\fR in the description string. Text between
343 two \fB\eb\fR (backspace) characters indicates that the text should be
344 emboldened when displayed. Text between two \fB\ea\fR (bell) characters
345 indicates that the text should be emphasized or italicized when displayed. Text
346 between two \fB\ev\fR (vertical tab) characters indicates that the text should
347 displayed in a fixed-width font. Text between two \fB\ef\fR (form feed)
348 characters is replaced by the output from the shell function whose name is that
349 of the enclosed text.
352 All output from this interface is written to the standard error.
355 There are several group types:
363 \fB[-[\fR\fIversion\fR][\fIflag\fR[\fInumber\fR\fB]]...[?\fR\fItext\fR\fB]]\fR
368 which appears as the first group enables the extended interface.
370 \fIversion\fR specifies the interface version, currently 1. The latest version
371 is assumed if version is omitted. Future enhancements can increment
372 \fIversion\fR, but all versions are supported. \fItext\fR typically specifies
373 an SCCS or CVS identification string. Zero or more flags with optional number
374 values can be specified to control option parsing. The flags are:
381 Cache this \fIoptstring\fR for multiple passes. Used to optimize built-ins that
382 can be called many times within the same process.
391 Ignore this \fIoptstring\fR when generating help. Used when combining
392 \fIoptstring\fR values from multiple passes.
401 Display only long option names in help messages.
410 The \fB-\fR option character prefix is optional. This supports the obsolete
411 \fBps\fR(1) option syntax.
420 The number specifies the number of \fB-\fR characters that must prefix long
421 option names. The default is \fB2\fR. \fB0\fR, \fB1\fR or \fB2\fR are accepted,
422 for example \fBp0\fR for \fBdd\fR(1M) and \fBp1\fR for \fBfind\fR(1).
431 The number specifies the manual page section number, \fB1\fR by default.
438 An option specification of the form
439 \fB[\fR\fIoption\fR\fB[!][=\fR\fInumber\fR\fB][:\fR\fIlongname\fR\fB][?\fR\fItext\fR\fB]]\fR.
440 In this case the first field is the option character, which is the value
441 returned in the name operand when the option is matched. If there is no option
442 character then a two or more digit number should be specified. This number is
443 returned as the value of the name operand if the long option is matched. If
444 option is followed by a \fB!\fR then the option character sense is the inverse
445 of the \fIlongname\fR sense. For options that do not take values \fBOPTARG\fR
446 is set to \fB0\fR for \fB!\fR inverted option characters and \fB1\fR
447 otherwise. \fI=number\fR optionally specifies a number to be returned in the
448 \fIname\fR operand instead of the option character. A \fIlongname\fR is
449 specified by \fB--longname\fR and is matched by the shortest non-ambiguous
450 prefix of all long options. An \fB*\fR in the \fBlongname\fR field indicates
451 that only characters up to that point need to match, provided any additional
452 characters match exactly. The enclosing \fB[\fR and \fB]\fR can be omitted for
453 an option that does not have a \fBlongname\fR or descriptive text.
458 An option argument specification. Options that take arguments can be
459 followed by \fB:\fR, indicating a string value or \fB#\fR, indicating a numeric
460 value, and an option argument specification. An option argument specification
461 consists of the option argument name as field 1. The remaining : separated
462 fields are a type name and zero or more of the special attribute words
463 \fBlistof\fR, \fBoneof\fR, and \fBignorecase\fR. A default option value can be
464 specified in the final field as :=default. The option argument specification
465 can be followed by a list of option value descriptions enclosed in braces. A
466 long option that takes an argument is specified as
467 \fB--longname=\fR\fIvalue\fR. If the \fB:\fR or \fB#\fR is followed by \fB?\fR,
468 the option argument is optional. If only the option character form is specified
469 then the optional argument value is not set if the next argument starts with
475 An option value description.
480 An argument specification. A list of valid option argument values can be
481 specified by enclosing them inside a \fB{...}\fR following the option argument
482 specification. Each of the permitted values can be specified with a \fB[...]\fR
483 containing the value followed by a description.
488 A group of the form \fB[+\fR\fB\e\fR\fIn\fR\fB\&...]\fR displays the
489 characters representing \fB\&...\fR in fixed-width font without adding line
495 A group of the form \fB[+\fR\fIname\fR\fB?\fR\fItext\fR\fB]\fR specifies a
496 section name with descriptive text. If \fIname\fR is omitted, \fItext\fR is
497 placed in a new paragraph.
502 A group of the form \fB[-\fR\fIname\fR\fB?\fR\fItext\fR\fB]\fR specifies
503 entries for the \fBIMPLEMENTATION\fR section.
507 If the leading character of \fIoptstring\fR is \fB+\fR, arguments beginning
508 with \fB+\fR are also be considered options.
511 A leading \fB:\fR character or a : following a leading \fB+\fR in
512 \fIoptstring\fR affects the way errors are handled. If an option character or
513 \fBlongname\fR argument not specified in \fIoptstring\fR is encountered when
514 processing options, the shell variable whose name is name is set to the \fB?\fR
515 character. The shell variable \fBOPTARG\fR is set to the character found. If an
516 option argument is missing or has an invalid value, then name is set to the
517 \fB:\fR character and the shell variable \fBOPTARG\fR is set to the option
518 character found. Without the leading \fB:\fR, \fIname\fR is set to the \fB?\fR
519 character, \fBOPTARG\fR is unset, and an error message is written to standard
520 error when errors are encountered.
523 The end of options occurs when:
527 The special argument \fB--\fR is encountered.
532 An argument that does not begin with a \fB-\fR is encountered.
537 A help argument is specified.
542 An error is encountered.
546 If \fBOPTIND\fR is set to the value \fB1\fR, a new set of arguments can be
550 \fBgetopts\fR can also be used to generate help messages containing command
551 usage and detailed descriptions. Specify \fIargs\fR as:
558 Use this to generate a usage synopsis.
567 Use this to generate a verbose usage message.
576 Use this to generate a formatted manual page.
585 Use this to generate an easy to parse usage message.
594 Use this to generate a man page in \fBhtml\fR format.
600 \fB\fB--??nroff\fR\fR
603 Use this to generate a man page in \fBnroff\fR format.
609 \fB\fB--??usage\fR\fR
612 Use this to list the current \fBoptstring\fR.
618 \fB\fB--???name\fR\fR
621 Use this to list \fBversion=\fR\fIn\fR, where \fIn\fR is greater than \fB0\fR,
622 if the option \fIname\fR is recognized by \fBgetopts\fR.
627 When the end of options is encountered, \fBgetopts\fR exits with a
628 \fBnon-zero\fR return value and the variable \fBOPTIND\fR is set to the index
629 of the first non-option argument.
634 The following options are supported by \fBksh93\fR:
638 \fB\fB-a\fR \fIname\fR\fR
641 Use \fIname\fR instead of the command name in usage messages.
647 The following operands are supported:
651 \fB\fIoptstring\fR\fR
654 A string containing the option characters recognised by the utility invoking
655 \fBgetopts\fR. If a character is followed by a colon, the option is expected to
656 have an argument, which should be supplied as a separate argument. Applications
657 should specify an option character and its option-argument as separate
658 arguments, but \fBgetopts\fR interprets the characters following an option
659 character requiring arguments as an argument whether or not this is done. An
660 explicit null option-argument need not be recognised if it is not supplied as a
661 separate argument when \fBgetopts\fR is invoked; see \fBgetopt\fR(3C). The
662 characters question-mark (\fB?\fR) and colon (\fB:\fR) must not be used as
663 option characters by an application. The use of other option characters that
664 are not alphanumeric produces unspecified results. If the option-argument is
665 not supplied as a separate argument from the option character, the value in
666 \fBOPTARG\fR is stripped of the option character and the \fB\(mi\fR\&. The
667 first character in \fIoptstring\fR determines how \fBgetopts\fR behaves if an
668 option character is not known or an option-argument is missing.
677 The name of a shell variable that is set by the \fBgetopts\fR utility to the
678 option character that was found.
683 The \fBgetopts\fR utility by default parses positional parameters passed to the
684 invoking shell procedure. If \fIarg\fRs are specified, they are parsed instead
685 of the positional parameters.
689 Since \fBgetopts\fR affects the current shell execution environment, it is
690 generally provided as a shell regular built-in. If it is called in a subshell
691 or separate utility execution environment, such as one of the following:
695 (getopts abc value "$@")
697 find . -exec getopts ... \e;
703 it does not affect the shell variables in the caller's environment.
706 Notice that shell functions share \fBOPTIND\fR with the calling shell even
707 though the positional parameters are changed. Functions that want to use
708 \fBgetopts\fR to parse their arguments usually want to save the value of
709 \fBOPTIND\fR on entry and restore it before returning. However, there are cases
710 when a function wants to change \fBOPTIND\fR for the calling shell.
713 \fBExample 1 \fRParsing and Displaying Arguments
716 The following example script parses and displays its arguments:
723 while getopts ab: name
729 ?) printf "Usage: %s: [-a] [-b value] args\en" $0
733 if [ ! -z "$aflag" ]; then
734 printf "Option -a specified\en"
736 if [ ! -z "$bflag" ]; then
737 printf 'Option -b "%s" specified\en' "$bval"
739 shift $(($OPTIND - 1))
740 printf "Remaining arguments are: %s\en" "$*"
745 \fBExample 2 \fRProcessing Arguments for a Command with Options
748 The following fragment of a shell program processes the arguments for a command
749 that can take the options \fB-a\fR or \fB-b\fR. It also processes the option
750 \fB-o\fR, which requires an option-argument:
764 shift `expr $OPTIND \(mi 1`
769 \fBExample 3 \fREquivalent Code Expressions
772 This code example accepts any of the following as equivalent:
777 cmd -a -b -o "xxx z yy" filename
778 cmd -a -b -o "xxx z yy" -- filename
779 cmd -ab -o xxx,z,yy filename
780 cmd -ab -o "xxx z yy" filename
781 cmd -o xxx,z,yy -b -a filename
786 .SH ENVIRONMENT VARIABLES
789 See \fBenviron\fR(5) for descriptions of the following environment variables
790 that affect the execution of \fBgetopts\fR: \fBLANG\fR, \fBLC_ALL\fR,
791 \fBLC_CTYPE\fR, \fBLC_MESSAGES\fR, and \fBNLSPATH\fR.
798 This variable is used by \fBgetopts\fR as the index of the next argument to be
808 This variable is used by \fBgetopts\fR to store the argument if an option is
815 The following exit values are returned:
822 An option, specified or unspecified by \fIoptstring\fR, was found.
831 The end of options was encountered or an error occurred.
837 The following exit values are returned by \fBksh93\fR:
844 A specified option was found.
853 An end of options was encountered.
862 A usage or information message was generated.
868 See \fBattributes\fR(5) for descriptions of the following attributes:
869 .SS "/usr/bin/getopts, sh, ksh"
877 ATTRIBUTE TYPE ATTRIBUTE VALUE
879 Interface Stability Committed
881 Standard See \fBstandards\fR(5).
892 ATTRIBUTE TYPE ATTRIBUTE VALUE
894 Interface Stability Uncommitted
900 \fBIntro\fR(1), \fBgetoptcvt\fR(1), \fBksh\fR(1), \fBksh93\fR(1), \fBps\fR(1),
901 \fBsh\fR(1), \fBgetopt\fR(3C), \fBattributes\fR(5), \fBenviron\fR(5),
906 Whenever an error is detected and the first character in the \fIoptstring\fR
907 operand is not a colon (\fB:\fR), a diagnostic message is written to standard
908 error with the following information in an unspecified format:
913 The invoking program name is identified in the message. The invoking program
914 name is the value of the shell special parameter \fB0\fR at the time the
915 \fBgetopts\fR utility is invoked. A name equivalent to
929 If an option is found that was not specified in \fIoptstring\fR, this error is
930 identified and the invalid option character is identified in the message.
936 If an option requiring an option-argument is found, but an option-argument is
937 not found, this error is identified and the invalid option character is
938 identified in the message.