diff --git a/bin/sh/sh.1 b/bin/sh/sh.1 --- a/bin/sh/sh.1 +++ b/bin/sh/sh.1 @@ -2359,27 +2359,58 @@ .It Ic getopts Ar optstring var The POSIX .Ic getopts -command. -The -.Ic getopts -command deprecates the older -.Xr getopt 1 -command. -The first argument should be a series of letters, each possibly +command parses command-line options and arguments. +The first argument +.Va optstring +should be a series of letters, each possibly followed by a colon which indicates that the option takes an argument. -The specified variable is set to the parsed option. +The specified variable +.Va var +is set to the parsed option. The index of the next argument is placed into the shell variable .Va OPTIND . If an option takes an argument, it is placed into the shell variable .Va OPTARG . -If an invalid option is encountered, +.Pp +If the first character of +.Va optstring +is not a colon +and if an invalid option (unspecified or missing a required argument) is encountered, +.Va OPTARG +is unset, +.Ar var +is set to +.Ql \&? +and a diagnostic message is written to stderr. +.Pp +If +.Va optstring +does start with a colon, invalid options give slightly different results: +.Bl -enum -offset indent +.It +.Va OPTARG +is set to the the option character found, +.It .Ar var is set to -.Ql \&? . -It returns a false value (1) when it encounters the end of the options. +.Ql \&: +when a required argument is missing or to +.Ql \&? +when the option was not specified, +.It +no diagnostic message is written to stderr. +.El +.Pp +.Ic getopts +returns a false value (1) when it encounters the end of the options. A new set of arguments may be parsed by assigning .Li OPTIND=1 . +The +.Ic getopts +command deprecates the older +.Xr getopt 1 +command. .It Ic hash Oo Fl rv Oc Op Ar command ... The shell maintains a hash table which remembers the locations of commands. With no arguments whatsoever, the