GETOPTS

Section: User Commands (1)
Updated: January 1988
Index Return to Main Contents
 

NAME

getopts - parse command options  

SYNOPSIS

getopts optstring name [arg ...]  

DESCRIPTION

getopts is used by shell procedures to parse positional parameters and to check for legal options. It supports all applicable rules of the command syntax standard (see Rules 3-10, intro(1)). It should be used in place of the getopt(1) command. (See the WARNING, below.)

optstring must contain the option letters the command using getopts will recognize; if a letter is followed by a colon, the option is expected to have an argument which should be separated from it by white space.

Each time it is invoked, getopts will place the next option in the shell variable name and the index of the next argument to be processed in the shell variable OPTIND. Whenever the shell or a shell procedure is invoked, OPTIND is initialized to 1.

When an option requires an option-argument, getopts places it in the shell variable OPTARG. If an illegal option is encountered, ? will be placed in name. When the end of the options is encountered, getopts exits with a non-zero exit status. The special option ``--'' may be used to delimit the end of the options. By default, getopts parses the positional parameters. If extra arguments (arg ...) are given on the getopts command line, getopts will parse them instead.

So all new commands will adhere to the command syntax standard described in intro(1), they should use getopts(1) or getopt(3C) to parse positional parameters and check for options that are legal for that command (see WARNINGS, below).  

EXAMPLE

The following fragment of a shell program shows how one might process the arguments for a command that can take the options a or b, as well as the option o, which requires an option-argument:

     case $c in
     a|b)      FLAGS=$FLAGS$c;;
     o)        OARG=$OPTARG;;
     \?)       echo $USAGE 1>&2
               exit 2;;
     esac
done
shift OPTIND-1
This code will accept any of the following as equivalent:

cmd -a -b -o "xxx z yy" file
cmd -a -b -o "xxx z yy" -- file
cmd -ab -o "xxx z yy" file
cmd -ab -o "xxx z yy" -- file
 

SEE ALSO

intro(1), sh(1).
getopt(3C) in the Programmer's Reference Manual.
UNIX System V Release 3.0 Release Notes.  

WARNING

Although the following command syntax rule (see intro(1)) relaxations are permitted under the current implementation, they should not be used because they may not be supported in future releases of the system. As in the EXAMPLE section above, a and b are options, and the option o requires an option-argument:

cmd -ab -oxxx file       (Rule 6 violation: there must be
          white space after an option that takes an option-argument)
Changing the value of the shell variable OPTIND or parsing different sets of arguments may lead to unexpected results.
 

DIAGNOSTICS

getopts prints an error message on the standard error output when it encounters an option letter not included in optstring.


 

Index

NAME
SYNOPSIS
DESCRIPTION
EXAMPLE
SEE ALSO
WARNING
DIAGNOSTICS

This document was created by man2html, using the manual pages.
Time: 23:47:21 GMT, February 06, 2023