GETOPT(1V)

NAME
getopt - parse command options in shell scripts

SYNOPSIS
set -- `getopt opstring $*`
set argv = (`getopt opstring $*`)

AVAILABILITY
This command is available with the System V software installation option. Refer to for information on how to install optional software.

DESCRIPTION
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 positional parameters ($1 $2...) of the shell are reset so that each option in optstring is broken out and preceded by a `-', along with the argument (if any) for each.

EXAMPLE

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:

    #! /usr/bin/sh
    set -- getopt abo: $*`
    if [ $? != 0 ]
    then
    	echo $USAGE
    	exit 2
    fi
    for i in $*
    do
    	case $i in
    	-a | -b) FLAG =$i; shift;;
    	-o)	OARG =$2; shift 2;;
    	--)	shift; break;;
    	esac
    done

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

SEE ALSO
csh(1), getopts(1), sh(1), getopt(3)

DIAGNOSTICS
getopt prints an error message on the standard error when it encounters an option letter not included in optstring.

NOTES
getopts(1) is preferred.