getopts

parse options from shell script command line 

KornShell Built-in


SYNOPSIS

getopts optstring name [arg ...]


DESCRIPTION

getopts obtains options and their arguments from a list of parameters that follows the standard POSIX.2 option syntax (that is, single letters preceded by a - and possibly followed by an argument value; the single letters may be grouped). 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).

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

cannot 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 (?) in name if it finds an option that does not appear in optstring, 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 next argument to be processed 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.

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 conditions:

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 optstring.

In addition to the standard POSIX.2 option syntax, getopts can also recognize option letters which are preceded by a +. There are two ways of handling such options.

If optstring begins with :+ or +, any options beginning with + set name to a string consisting of + followed by the option letter. The OPTARG variable is set as usual for options that take an argument.

If optstring begins with :++ or ++ or + appears anywhere in optstring other than the start, any options beginning with + set name to + and the OPTARG variable to the option letter. With this method, it is not possible to have options beginning with + that take arguments.


EXAMPLE

This is an example of using getopts in a shell script. Compare it to the getopt example.

# 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="\t"	# 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

OPTARG 

stores the value of the option argument found by getopts.

OPTIND 

contains the index of the next argument to be processed.


DIAGNOSTICS

Possible exit status values are:

0 

getopts found a script command line argument 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 invalid command line option.


PORTABILITY

POSIX.2. x/OPEN Portability Guide 4.0. Windows XP. Windows Server 2003. Windows Vista. Windows 7. Windows Server 2008. Windows 8. Windows Server 2012.

On UNIX systems, getopts is built into both the KornShell and Bourne Shell.


NOTE

This command is built into the MKS KornShell.


AVAILABILITY

PTC MKS Toolkit for Power Users
PTC MKS Toolkit for System Administrators
PTC MKS Toolkit for Developers
PTC MKS Toolkit for Interoperability
PTC MKS Toolkit for Professional Developers
PTC MKS Toolkit for Enterprise Developers
PTC MKS Toolkit for Enterprise Developers 64-Bit Edition
PTC Integrity


SEE ALSO

Commands:
getopt, sh


PTC MKS Toolkit 9.6 patch 1 Documentation Build 5.