getopts : getopts optstring name [arg]
Parameters for the parse argument.
Getopts is used by shell routines to analyze positional parameters as options.
OPTSTRING contains the letters of the options to be recognized; if the letter is followed by a colon, it is expected that the parameter will have an argument, which should be separated by a space.
Each time it is called, getopts places the next option in the shell variable $ name, the initialization name if it does not exist, and the index of the next argument to be processed in the shell is the OPTIND variable. OPTIND is initialized to 1 each time a shell or shell script is called. If an argument is required for the parameter, getopts places this argument in the OPTARG shell OPTARG .
getopts reports errors in one of two ways. If the first character of OPTSTRING is a colon, getopts uses silent error reporting. In this mode, error messages are not printed. If an invalid option is seen, getopts places the option character in OPTARG. If the required argument is not found, getopts places in NAME ' : ' sets OPTARG to the option character. If getopts is not in silent mode and an invalid option, getopts places '?' in NAME and unsets OPTARG. If the required argument is not found, ' ? is placed in NAME, OPTARG does not work, and a diagnostic message is printed.
If the OPTERR shell variable is OPTERR to 0, getopts disables printing of error messages, even if the first character of OPTSTRING is not a colon. By default, OPTERR is set to 1.
Getopts usually parses positional parameters ($ 0 - $ 9), but if there are more arguments, they are parsed instead.
Exit Status:
Returns success if an option is found; if the end of the options occurs or an error occurs.