That's the standard convention — some GNU utilities accept options after arguments, but the normal thing is that in somecommand foo -bar qux , -bar is not parsed as an option. Wrong. is placed in name, OPTARG is unset, and a diagnostic message is printed. All getopts does is provide an easy way to have a flag-style input. When getopts obtains an option from the script command line, it stores the index of the next argument to be processed in the shell variable OPTIND. is: no_argument (or 0) if the option does not take an argument; required_argument (or 1) if the option requires an argument; or optional_argument (or 2) if the option takes an optional argument. getoptがoption argumentを持つoptionを解析すると、 optargにoption argumentへのポインタが設定されます。 optargを参照することでoption argumentを取得できます。 optind optindはgetoptが次に処理するargv配列のindexです。初期値は Actually getopts does support optional arguments! separated by whitespace on the command line). shift is a shell builtin which moves the positional parameters of the script down a specified number of positions provided to it as a positive number, discarding the related arguments. It is designed to process command line arguments that follow the POSIX Utility Syntax Guidelines, based on the C interface of getopt. . public class Getoptextends Object This is a Java port of GNU getopt, a class for parsing command line arguments passed to programs. From the bash man page: If a required argument is not found, and getopts is not silent, a question … are specified on the getopts command line, getopts parses them instead. An option character in this string can be followed by a colon (‘:’) to indicate that it takes a required argument. 1. Firstly, according to the code getopts argument list (:s:dvu:p:n:), -s, -u, -p, and -n take a mandatory argument. The symbolic constants are macros for the numeric values given in the table. If the option has an optional argument, it If the option has a required argument, it may be written directly after the option character or as the next parameter (ie. If a value is required, it does not matter whether the value has leading white space or not. /usr/lib/getoptcvt reads the shell script in filename , converts it to use getopts instead of getopt , and writes the results on the standard output. If extra arguments (argument. The getopt() function is a builtin function in C and is used to parse command line arguments. See note. with a getopts line in my script of getopts p:cs opt a command line of