getopts |
---|
|
getopts option_spec variable [ arguments ... ] |
---|
|
To simplify argument processing, and make it possible for shell scripts to easily adhere to POSIX argument processing conventions. |
---|
|
None. |
---|
|
When called repeatedly (such as in a while loop), step through the provided command-line arguments, or "$@" if none, exiting nonzero at -- or the first nonoption argument, or upon error. The option_spec describes options and their arguments; see the text for details. |
---|
For each valid option, set variable to the option letter. If the option has an argument, the argument value is placed in OPTARG. At the end of processing, OPTIND is set to the number of the first nonoption argument. See text for further details. |
---|
|
The ksh93 version of getopts conforms to POSIX, but has many additional features. See the ksh93 documentation and Learning the Korn Shell (O'Reilly). |
---|
|
The first argument to getopts is a string listing valid option letters. If an option letter is followed by a colon, then that option requires an argument, which must be supplied. Upon encountering such an option, getopts places the argument value into the variable OPTARG. The variable OPTIND contains the index of the next argument to be processed. The shell initializes this variable to 1. |
---|
The second argument is a variable name. This variable is updated each time getopts is called; its value is the found option letter. When getopts finds an invalid option, it sets the variable to a question mark character. Here is the previous example, using getopts: |
---|