How to Use getopts in Bash


In Linux, tasks can be automated by writing several commands in one script and then executing the script whenever the same set of commands are needed. These scripts accept a variety of command-line arguments, which are often passed collectively when the script is executed.

The good thing is that a built-in function (called getopts) is available in Linux that is used to parse these command-line arguments. In this tutorial, you will learn about the getopts function in detail and how to use it to handle command-line arguments very effectively. This is explained with the help of several examples.

The getopts function is used to parse the arguments that are passed to a script through the command line. In other words, getopts look for all the options that your script might take in. Keep in mind that when the options are passed to a script, they are passed using a dash (-). This is analogous to using options when you use Linux commands. For instance, you can consider the example of ls or cd commands which take a variety of options to provide different outputs (e.g., ls -l).

Similarly, getopts is used by shell procedures to split (parse) options provided on command lines and to check for legal options. In simple words, getopts look for the options mentioned with a dash (-) when the script is executed. To do this, it uses the GNU getopt (3)routines.


The syntax of getopts is explained below.

getopts [getopt_options] [-o options] [–] [optstring] [parameters]

It consists of five different parts, which are explained below

  1. The command itself i.e., getopts
  2. The getopt_options, which describes different options of how you want to parse the arguments. They may be single-dash long options or double dash options. The getopt_options is explained in more detail below with several examples.
  3. The short options i.e., -o or --options. Just like the Form first syntax but with the option -o and before the -- (double dash).
  4. --, separates the getopt_options from the options you want to parse and the allowed short options.
  5. The parameters are the options that you have passed to the script. The options you want to parse and get the actual values set on them.

These are the letters that would be passed as options to your script by using a dash (like ls -l). A colon (:) can be used before and after these letters. A user may enter any other letter which is not specified in getopts. In this case, a colon is used before the option letters (like :a). In technical terms, the colon before the letter turns off all errors and warnings getopts command may receive. The colon after the letter (such as a:) means that an argument should be provided to this option.

Let’s dive in deeply to understand how to use the above parts of getopts.

Variables of getopts

There are two variables provided by getopts command, which are $OPTIND and $OPTARG.

The optional index ($OPTIND) variable is the index to the next argument that needs to be processed. It is important to note that getopts does not change the position of the options, so you need to use $OPTIND to shift the next positional parameter.

The optional argument ($OPTARG) variable is the argument that you passed through the command line.

How does it work?

Every time you run getopts, it checks for one of the options defined in optstring. If it finds any, it places the option letter in a variable called optname. If the option does not match those set in optstring, getopts assigns a question mark (“?”) to the optname variable. The question mark represents any other than the specified options. The use of “?” is explained in the example section below.

To run the getopts command, again and again, it is mostly used with a while loop to parse the arguments. A loop is necessary to use because there is more than one option and you want all the options that the user will provide to be processed.

Within the while loop, a case statement is used that specifies several options to match. The appropriate actions mentioned in each case option would be performed for the matching argument. The overall structure where getopts is used would look like the script shown below.

#! /bin/bash

while getopts a:b: options; do
        case $options in
                a) ap=$OPTARG;;
                b) bo=$OPTARG;;

echo "Option A=$ap and Option B=$bo"

In this chunk of script, a and b are options that would be passed to the script. Whatever option is used it would be stored in the OPTIONS variable and matched in the case block to perform the appropriate action. You may have noticed that here $OPTARG is also used in this example, which is a variable provided by getopts.

Examples of using getopts

Suppose the script shown above is stored as “”, then it can be called in the following ways. The output of these commands is also shown in brackets next to them.

sh -a Apple -b Boy

word image 36


Note if a script is executed without options (like $ sh or in a wrong format (like $ sh oooo), you will not get any benefit of using getopts.

word image 37

What if the user follows the right format but enters the wrong option which is not specified in the script? To handle such a situation, you have to edit the above script. First add a colon in front of the option letters (like :a) and then add a ? to represent any other options entered by the user.

word image 38

In the example above, the user will get the error message if she enters any letter other than a and b.

word image 39


You can use the getopts command to parse command-line arguments. Using this function different tasks can be performed based on the input options provided by the user. If you still have some problems, feel free to ask.

Notify of
Receive notifications when your comment receives a reply. (Optional)
Your username will link to your website. (Optional)

Inline Feedbacks
View all comments
You May Also Like