1.Dd June 21, 1993 2.Dt GETOPT 1 3.Os 4.Sh NAME 5.Nm getopt 6.Nd parse command options 7.Sh SYNOPSIS 8.Nm set \-\- \`getopt optstring $*\` 9.Sh DESCRIPTION 10.Nm Getopt 11is used to break up options in command lines for easy parsing by 12shell procedures, and to check for legal options. 13.Op Optstring 14is a string of recognized option letters (see 15.Xr getopt 3 16); 17if a letter is followed by a colon, the option 18is expected to have an argument which may or may not be 19separated from it by white space. 20The special option 21.B \-\- 22is used to delimit the end of the options. 23.Nm Getopt 24will place 25.B \-\- 26in the arguments at the end of the options, 27or recognize it if used explicitly. 28The shell arguments 29(\fB$1 $2\fR ...) are reset so that each option is 30preceded by a 31.B \- 32and in its own shell argument; 33each option argument is also in its own shell argument. 34.Sh EXAMPLE 35The following code fragment shows how one might process the arguments 36for a command that can take the options 37.Op a 38and 39.Op b , 40and the option 41.Op o , 42which requires an argument. 43.Pp 44.Bd -literal -offset indent 45set \-\- \`getopt abo: $*\` 46if test $? != 0 47then 48 echo 'Usage: ...' 49 exit 2 50fi 51for i 52do 53 case "$i" 54 in 55 \-a|\-b) 56 flag=$i; shift;; 57 \-o) 58 oarg=$2; shift; shift;; 59 \-\-) 60 shift; break;; 61 esac 62done 63.Ed 64.Pp 65This code will accept any of the following as equivalent: 66.Pp 67.Bd -literal -offset indent 68cmd \-aoarg file file 69cmd \-a \-o arg file file 70cmd \-oarg -a file file 71cmd \-a \-oarg \-\- file file 72.Ed 73.Sh SEE ALSO 74.Xr sh 1 , 75.Xr getopt 3 76.Sh DIAGNOSTICS 77.Nm Getopt 78prints an error message on the standard error output when it 79encounters an option letter not included in 80.Op optstring . 81.Sh HISTORY 82Written by Henry Spencer, working from a Bell Labs manual page. 83Behavior believed identical to the Bell version. 84.Sh BUGS 85Whatever 86.Xr getopt 3 87has. 88.Pp 89Arguments containing white space or imbedded shell metacharacters 90generally will not survive intact; this looks easy to fix but isn't. 91.Pp 92The error message for an invalid option is identified as coming 93from 94.Nm getopt 95rather than from the shell procedure containing the invocation 96of 97.Nm getopt ; 98this again is hard to fix. 99.Pp 100The precise best way to use the 101.Nm set 102command to set the arguments without disrupting the value(s) of 103shell options varies from one shell version to another. 104