getopt - parse command options in shell scripts
set -- `getopt opstring $*`
set argv = (`getopt opstring $*`)
This command is available with the System V software installation
option. Refer to for information on how to install optional software.
getopt breaks up options in command lines for easy parsing by shell
scripts, and checks for legal options. optstring is a string of option
letters to recognize, (see getopt(3)). If a letter is followed by a
colon, the option is expected to have an argument -- which may or may
not be separated by white space.
(The `--' following set indicates that the Bourne shell is to pass
arguments beginning with a dash as parameters to the script.)
If `-' appears on the command line that invokes the script, getopt uses
it to delimit the end of options it is to parse (see example below).
If used explicitly, getopt will recognize it; otherwise, getopt will
generate it at the first argument it encounters that has no `-'. In
either case, getopt places it at the end of the options. The posi-
tional parameters ($1 $2...) of the shell are reset so that each option
in optstring is broken out and preceded by a `-', along with the argu-
ment (if any) for each.
The following code fragment shows how one might process the arguments
for a command that can take the options a or b, as well as the option
o, which requires an argument:
set -- getopt abo: $*`
if [ $? != 0 ]
for i in $*
case $i in
-a | -b) FLAG =$i; shift;;
-o) OARG =$2; shift 2;;
--) shift; break;;
This code will accept any of the following command lines as equivalent:
cmd -a -o arg f1 f2
cmd -aoarg f1 f2
cmd -oarg -a f1 f2
cmd -a -oarg -- f1 f2
csh(1), getopts(1), sh(1), getopt(3)
getopt prints an error message on the standard error when it encounters
an option letter not included in optstring.
getopts(1) is preferred.
17 September 1989 GETOPT(1V)