The getopts shell builtin

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 and getopt
  • 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:

  • getopt does not handle empty flag arguments well; getopts does
  • getopts is included in the Bourne shell and Bash; getopt needs to be installed separately
  • getopt allows for the parsing of long options (--help instead of -h); getopts does not
  • getopts has a simpler syntax; getopt is more complicated (mainly because it is an external program and not a builtin)

In general, the consensus seems to be that for most cases, using getopts is preferable (unless you really want long options). Since getopts is a Bash builtin, we'll use it as well, especially since we do not have the need for long options.

Most commands you use on the Terminal have both the short option (which is used almost always when interactively working at the Terminal to save time) and the long option (which is more descriptive and is more suitable for creating better readable scripts). In our experience, short options are more prevalent and, when used correctly, more recognizable as well.

The following list shows the most common short flags, which do the same to most commands:

  • -h: Prints the help/usage for a command
  • -v: Makes the command verbose
  • -q: Makes the command quiet
  • -f <file>: Passes a file to the <indexentry content="getopts shell builtin, flags:-f ">command
  • -r: Performs the operation recursively
  • -d: Runs the command in debug mode
Do not assume all commands parse the short flags, as specified previously. While this is true for most commands, don't all follow these trends. What is printed here has been found from personal experience and should always be verified by you before running a command that is new to you. That being said, running a command without arguments/flags or with a -h will, at least 90% of the time, print the correct usage for you to admire.

Even though it would have been nice to have long options available to us for our getopts scripting, even long options are never a substitute for writing readable scripts and creating good hints for the user that is using your script. We feel that that's much more important than having long options! Besides, the getopts syntax is much cleaner than a comparable getopt, and adhering to the KISS principle is still one of our goals.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.137.212.212