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).
case $c in a|b) FLAGS=$FLAGS$c;; o) OARG=$OPTARG;; \?) echo $USAGE 1>&2 exit 2;; esac done shift OPTIND-1This 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
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.