MPE/iX Shell and Utilities Reference Manual, Vol 1
getopts(1) MPE/iX Shell and Utilities getopts(1)
NAME
getopts — parse options from shell script command line
SYNOPSIS
getopts optstring name [arg ...]
DESCRIPTION
getopts obtains options and their arguments from a list of parameters that follows the stan-
dard
POSIX.2 option syntax (that is, single letters preceded by a – and possibly followed by an
argument value). Typically, shell scripts use getopts to parse arguments passed to them.
When you specify args on the getopts command line, getopts parses those arguments
instead of the script command line (see set(1)).
The optstring gives all the option letters that the script recognizes. For example, if the script
recognizes –a, –f, and –s, optstring is afs. If you want an option letter to be followed by an
argument value or group of values, put a colon after the letter, as in a:fs. This indicates that
getopts expects the –a option to have the form
–a value
Normally one or more blanks separate the value from the option letter; however, getopts
also handles values that follow the letter immediately, as in
–avalue
optstring can not contain the question mark (?) character.
The name on the getopts command line is the name of a shell variable. Each time you
invoke getopts, it obtains the next option from the positional parameters and places the
option letter in the shell variable name.
getopts places a question mark (?)inname if it finds an option that does not appear in opt-
string, or if an option value is missing.
Each option on the script command line has a numeric index. The first option found has an
index of 1, the second has an index of 2, and so on. When getopts obtains an option from
the script command line, it stores the index of the script in the shell variable OPTIND.
When an option letter has an associated argument (indicated with a : in optstring), getopts
stores the argument as a string in the shell variable OPTARG. If an option doesn’t take an
argument or getopts expects an argument but doesn’t find one, getopts unsets OPTARG.
1-256 Commands and Utilities