Now the real fun begins! In this second part of this chapter, we'll explain the getopts
shell builtin. The getopts
command is used in the beginning of your script to get the options you supplied in the form of flags. It has a very specific syntax that will seem confusing at first, but, once we've looked at it fully, it should not be too complicated for you to understand.
Before we dive in, though, we'll need to discuss two things:
- The difference between
getopts
andgetopt
- Short versus long options
As stated, getopts
is a shell builtin. It is available in both the regular Bourne shell (sh
) and in Bash. It originated around 1986, as a replacement for getopt
, which was created sometime before 1980.
In contrast to getopts
, getopt
is not built into the shell: it is a standalone program that has been ported to many different Unix and Unix-like distributions. The main differences between getopts
and getopt
are as follows: