NAME
getopts — parse
utility options
SYNOPSIS
getopts |
optstring name [arg ...] |
DESCRIPTION
The getopts utility can be used to
retrieve options and option-arguments from a list of parameters. It shall
support the utility argument syntax guidelines 3 through 10, inclusive,
described in 2.10.2. Each time it is invoked, the
getopts utility shall place the value of the next
option in the shell variable specified by the name operand and the index of
the next argument to be processed in the shell variable
OPTIND. Whenever the shell is invoked,
OPTIND shall be initialized to 1. When the option requires
an option-argument, the getopts utility shall place
it in the shell variable OPTARG. If no option was found,
or if the option that was found does not have an option-argument,
OPTARG shall be unset.
If an option character not contained in the optstring operand is
found where an option character is expected, the shell variable specified by
name shall be set to the question-mark (?) character. In this case, if the
first character in optstring is a colon (:), the shell variable
OPTARG shall be set to the option character found, but no
output shall be written to standard error; otherwise, the shell variable
OPTARG shall be unset and a diagnostic message shall be
written to standard error. This condition shall be considered to be an error
detected in the way arguments were presented to the invoking application,
but shall not be an error in getopts processing. If
an option-argument is missing:
- If the first character of optstring is a colon, the shell variable specified by name shall be set to the colon character and the shell variable OPTARG shall be set to the option character found.
- Otherwise, the shell variable specified by name shall be set to the
question-mark character, the shell variable OPTARG shall
be unset, and a diagnostic message shall be written to standard error.
This condition shall be considered to be an error detected in the way
arguments were presented to the invoking application, but shall not be an
error in
getoptsprocessing; a diagnostic message shall be written as stated, but the exit status shall be zero.
When the end of options is encountered, the
getopts utility shall exit with a return value
greater than zero; the shell variable OPTIND shall be set
to the index of the first nonoption-argument, where the first -- argument is
considered to be an option-argument if there are no other
nonoption-arguments appearing before it, or the value $# + 1 if there are no
nonoption-arguments; the name
variable shall be set to the question-mark character. Any of the
following shall identify the end of options: the special option --, finding
an argument that does not begin with a -, or encountering an error. The
shell variables OPTIND and OPTARG shall
be local to the caller of getopts and shall not be
exported by default. The shell variable specified by the name operand,
OPTIND, and OPTARG shall affect the
current shell execution environment; see 3.12. If the application sets
OPTIND to the value 1, a new set of parameters can be
used: either the current positional parameters or new arg values. Any other
attempt to invoke getopts multiple times in a single
shell execution environment with parameters (positional parameters or arg
operands) that are not the same in all invocations, or with an
OPTIND value modified to be a value other than 1, produces
unspecified results.
OPTIONS
None.
OPERANDS
The following operands shall be supported by the implementation:
- optstring
-
A string containing the option characters recognized by the utility invoking
getopts. If a character is followed by a colon, the option shall be expected to have an argument, which should be supplied as a separate argument. Applications should specify an option character and its option-argument as separate arguments, butgetoptsshall interpret the characters following an option character requiring arguments as an argument whether or not this is done. An explicit null option-argument need not be recognized if it is not supplied as a separate argument whengetoptsis invoked. [See also thegetopt() Description in B.7]. The characters question-mark and colon shall not be used as option characters by an application. The use of other option characters that are not alphanumeric produces unspecified results. If the optionargument is not supplied as a separate argument from the option character, the value in OPTARG shall be stripped of the option character and the ’-’. The first character in optstring shall determine howgetoptsshall behave if an option character is not known or an option-argument is missing. See DESCRIPTION . - name
-
The name of a shell variable that shall be set by the
getoptsutility to the option character that was found. See DESCRIPTION . Thegetoptsutility by default shall parse positional parameters passed to the invoking shell procedure. If args are given, they shall be parsed instead of the positional parameters.
STANDARD INPUT
None.
INPUT FILES
None.
ENVIRONMENT VARIABLES
The following environment variables shall affect the execution of
getopts:
LANG-
This variable shall determine the locale to use for the locale categories when both LC_ALL and the corresponding environment variable (beginning with LC_ ) do not specify a locale. See 2.6.
LC_ALL-
This variable shall determine the locale to be used to override any values for locale categories specified by the settings of LANG or any environment variables beginning with LC_.
LC_CTYPE-
This variable shall determine the locale for the interpretation of sequences of bytes of text data as characters (e.g., single- versus multibyte characters in arguments).
LC_MESSAGES-
This variable shall determine the language in which messages should be written.
OPTIND-
This variable shall be used by the
getoptsutility as the index of the next argument to be processed.
ASYNCHRONOUS EVENTS
Default.
STANDARD OUTPUT
None.
STANDARD ERROR
Whenever an error is detected and the first character in the optstring operand is not a colon (:), a diagnostic message shall be written to standard error with the following information in an unspecified format:
- The invoking program name shall be identified in the message. The invoking
program name shall be the value of the shell special parameter 0 (see
3.5.2) at the time the
getoptsutility is invoked. A name equivalent to
basename "$0"
may be used.
- If an option is found that was not specified in optstring, this error shall be identified and the invalid option character shall be identified in the message.
- If an option requiring an option-argument is found, but an option-argument is not found, this error shall be identified and the invalid option character shall be identified in the message.
OUTPUT FILES
None.
EXTENDED DESCRIPTION
None.
EXIT STATUS
The getopts utility shall exit with one of
the following values:
CONSEQUENCES OF ERRORS
Default.
RATIONALE
EXAMPLES
The getopts utility was chosen in
preference to the getopt utility specified in System V because
getopts handles option-arguments containing
<blank> characters. Since getopts affects the
current shell execution environment, it is generally provided as a shell
regular built-in. If it is called in a subshell or separate utility
execution environment, such as one of the following:
( getopts abc value "$@") nohup
getopts ... find . -exec
getopts ... \;
it will not affect the shell variables in the caller’s environment.
Note that shell functions share OPTIND with the
calling shell even though the positional parameters are changed. Functions
that want to use getopts to parse their arguments
will usually want to save the value of OPTIND on entry and
restore it before returning. However, there will be cases when a function
will want to change OPTIND for the calling shell. The
following example script parses and displays its arguments: aflag= bflag=
while getopts ab: name do case $name in a) aflag=1;;
b) bflag=1 bval="$ OPTARG ";; ?) printf
"Usage: %s: [-a] [-b value] args\n" $0 exit 2;; esac done if [ !
-z "$aflag" ]; then printf "Option -a specified\n" fi if
[ ! -z "$bflag" ]; then printf ’Option -b "%s"
specified\n’ "$bval" fi shift $(($ OPTIND
- 1)) printf "Remaining arguments are: %s\n"
"$∗"
HISTORY OF DECISIONS MADE
The OPTARG variable is not mentioned in the
Environment Variables subclause because it does not affect the execution of
getopts; it is one of the few
‘‘outputonly’’ variables used by the standard
utilities. Use of colon (:) as an option character (in a previous draft) was
new behavior and violated the syntax guidelines. Many objectors felt that it
did not add enough to getopts to warrant mandating
the extension to existing practice. The colon is now specified to behave as
in the KornShell version of the getopts utility;
when used as the first character in the optstring operand, it disables
diagnostics concerning missing option-arguments and unexpected option
characters. This replaces the use of the
OPTERR
variable that was specified in an earlier draft. The formats of the
diagnostic messages produced by the getopts utility
and the
getopt()
function are not fully specified because implementations with superior
(‘‘friendlier’’) formats objected to the formats
used by some historical implementations. It was felt to be important that
the information in the messages used be
uniform between getopts and
getopt().
Exact duplication of the messages might not be possible, particularly if a
utility is built on another system that has a different
getopt() function, but the messages must have
specific information included so that the program name, invalid option
character, and type of error can be distinguished by a user.
Only a rare application program will intercept a
getopts standard error message and want to parse it.
Therefore, implementations are free to choose the most usable messages they
can devise. The following formats are used by many historical
implementations:
"%s: illegal option -- %c\n", <program name>, <option character>
"%s: option requires an argument -- %c\n", <program name>, <option character>
Historical shells with built-in versions of
getopt()
or getopts have used different formats, frequently
not even indicating the option character found in error.