Getopts
Encyclopedia
getopts
is a built-in Unix shell command for parsing command-line arguments.It is designed to process command line arguments that follow the POSIX Utility Syntax Guidelines.
History
Getopts was first introduced in 1986 in the Bourne shellBourne shell
The Bourne shell, or sh, was the default Unix shell of Unix Version 7 and most Unix-like systems continue to have /bin/sh - which will be the Bourne shell, or a symbolic link or hard link to a compatible shell - even when more modern shells are used by most users.Developed by Stephen Bourne at AT&T...
shipped with Unix SVR3. Earlier versions of the Bourne shell did not have getopts. Note: getopts is not the same as getopt.
Currently, getopts is available in a number of shells, including: Bourne shell
Bourne shell
The Bourne shell, or sh, was the default Unix shell of Unix Version 7 and most Unix-like systems continue to have /bin/sh - which will be the Bourne shell, or a symbolic link or hard link to a compatible shell - even when more modern shells are used by most users.Developed by Stephen Bourne at AT&T...
, Korn shell
Korn shell
The Korn shell is a Unix shell which was developed by David Korn in the early 1980s and announced at USENIX on July 14, 1983. Other early contributors were AT&T Bell Labs developers Mike Veach, who wrote the emacs code, and Pat Sullivan, who wrote the vi code...
and Bash (Unix shell).
getopt (original)
The getopts was developed as an improvement to the original getoptGetopt
getopt is a C library function used to parse command-line options.It is also the name of a Unix program for parsing command line arguments in shell scripts.- History :A long standing issue with command line programs was how to specify options;...
Unix program.
The original getopt program has less features than getopts. For example: it did not support whitespaces in arguments, and there was no ability to disable the output of error messages.
The original getopt program was a external command line program (unlike getopts which is a built-in shell command).
getopt (GNU enhanced)
An alternative to getopts is the GNU enhanced version of getoptGetopt
getopt is a C library function used to parse command-line options.It is also the name of a Unix program for parsing command line arguments in shell scripts.- History :A long standing issue with command line programs was how to specify options;...
. Both being improvements to the original getopt Unix program.
The GNU enhanced version of getopt supports the extra features of getopts plus more advanced features. Most significantly, the GNU enhanced version of getopt supports long option names (e.g.
--help
) and the options do not have to appear before all the operands (e.g. command option1 option2 option3 -a -b
is permitted by the GNU enhanced version of getopt but does not work as expected with getopts).The GNU enhanced version of getopt is an external command line program (unlike getopts which is a built-in shell command).