getopts - Parse utility options

Format

getopts opstring name [arg ...]

Description

getopts obtains options and their arguments from a list of parameters that follows the standard POSIX.2POSIX.2 option syntax (that is, single letters preceded by a hyphen (-) and possibly followed by an argument value). Typically, shell scripts use getopts to parse arguments passed to them. When you specify arguments with the arg argument on the getopts command line, getopts parses those arguments instead of the script command-line arguments (see set).

Options

opstring
Gives all the option letters that the script recognizes. For example, if the script recognizes -a, -f, and s, opstring 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 value from the option letter; however, getopts also handles values that follow the letter immediately, as in -avalue. opstring cannot contain a question mark (?) character.
name
Specifies 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 (?) in name if it finds an option that does not appear in opstring, or if an option value is missing.

arg ...
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 a following argument (indicated with a : in opstring), getopts stores the argument as a string in the shell variable OPTARG. If an option doesn't take an argument, or if getopts expects an argument but doesn't find one, getopts unsets OPTARG.

When getopts reaches the end of the options, it exits with a status value of 1. It also sets name to the character ? and sets OPTIND to the index of the first argument after the options. getopts recognizes the end of the options by any of the following situations:
  • Finding an argument that doesn't start with -
  • Finding the special argument --, marking the end of options
  • Encountering an error (for example, an unrecognized option letter)

OPTIND and OPTARG are local to the shell script. If you want to export them, you must do so explicitly. If the script invoking getopts sets OPTIND to 1, it can call getopts again with a new set of parameters, either the current positional parameters or new arg values.

By default, getopts issues an error message if it finds an unrecognized option or some other error. If you do not want such messages printed, specify a colon as the first character in opstring.

Examples

Following is an example of using getopts in a shell script:
# Example illustrating use of getopts builtin. This
# shell script would implement the paste command,
# using getopts to process options, if the underlying
# functionality was embedded in hypothetical utilities
# hpaste and vpaste, which perform horizontal and
# vertical pasting respectively.
#
paste=vpaste    # default is vertical pasting
seplist=" " # default separator is tab

while getopts d:s o
do      case "$o" in
        d)      seplist="$OPTARG";;
        s)      paste=hpaste;;
        [?])  print >&2 "Usage: $0 [-s] [-d seplist] file ..."
                exit 1;;
        esac
done
shift $OPTIND-1

# perform actual paste command
$paste -d "$seplist" "$@"

Environment variables

getopts uses the following environment variables:
OPTARG
Stores the value of the option argument found by getopts.
OPTIND
Contains the index of the next argument to be processed.

Localization

getopts uses the following localization environment variables:
  • LANG
  • LC_ALL
  • LC_CTYPE
  • LC_MESSAGES

Usage notes

getopts is a built-in shell command.

Exit values

0
getopts found a script command line with the form of an option. This happens whether or not it recognizes the option.
1
getopts reached the end of the options, or an error occurred.
2
Failure because of an incorrect command-line option

Portability

POSIX.2, X/Open Portability Guide.

On UNIX systems, getopts is built in both the KornShell and Bourne shell.

Related information

sh