getopts Command in Linux



getopts is a Linux command used in shell scripts to parse command-line options and arguments. It helps scripts handle flags and their associated values in a standardized way.

With the help of getopts command, you can make your scripts more user-friendly and robust. Thus, simplifying the process of validating and processing user inputs. This command is pretty useful for automation tasks and system administration, where handling various input options efficiently is crucial.

Table of Contents

Here is a comprehensive guide to the options available with the getopts command −

Syntax of getopts Command

The basic syntax to use the getopts command on Linux is given below −

getopts optstring varname [args]

Where,

  • optstring is a string containing the option characters. If an option character is followed by a colon (:), it expects an argument.
  • varname is the name of the variable that will hold the current option.

getopts Command Options

The followings are some basic options you can use with the getopts command in Linux −

Option Description
-a Adds the options to the option string specified by -o.
-n Defines the variable name where the next option will be stored.
-o Specifies a list of single-character options to be parsed.
-q Suppresses the error messages from getopts.
-s Sets the OPTARG variable to the option’s argument.
-u Clears the list of options and allows getopts to be reused.

Examples of getopts Command in Linux

To use getopts command on Linux, first you must create a script on your system. As an example, we have created the following script, save it with the name script.sh. Inside the script, we use the following code −

#!/bin/bash

while getopts "ab" opt; do
   case $opt in
      a) echo "Option -a";;
      b) echo "Option -b";;
      \?) echo "Invalid option: -$OPTARG";;
   esac
done

The script executes a while loop that processes arguments matching the specified optstring, "a:b:". It assigns the flag's value to the variable opt. If the flag has an associated argument, it is stored in the variable OPTARG.

The optstring function works as follows −

  • For each option letter, getopts stores the option in the variable opt (declared immediately after the optstring) and iterates through the loop.
  • Any option letter followed by a colon expects an argument, which is stored in the variable OPTARG.
  • If getopts expects an argument but cannot parse one, it prints an error. If no argument is expected, OPTARG is set to an empty string ("").
  • If the first character of the optstring is a colon (":"), the error message is suppressed.

Once you add your desired code, save your script and make it executable by using the chmod command −

chmod +x script.sh

After you have created the script, it’s now time to run it by passing the arguments as expected −

./script.sh -ab

In this example, the script is executed with the options -a and -b. Here's what happens step-by-step −

  • Option Parsing − The getopts command processes each option provided in the command line.
  • Option -a − The script recognizes the -a option and executes the corresponding action defined in the case statement.
  • Option -b − Similarly, the script recognizes the -b option and executes its corresponding action.

Expected Output

When you run the script with -a and -b, the output will be −

Examples of getopts Command1

Explanation

  • Option -a − The script prints "Option -a" because the case statement includes a) echo "Option -a";;.
  • Option -b − The script prints "Option -b" because the case statement includes b) echo "Option -b";;

Additional Notes

Invalid Options − If an invalid option is provided, the script will print an error message. For example, running ./script.sh -c will output "Invalid option: -c".

Examples of getopts Command2

Arguments for Options − If options require arguments, they can be passed in the command line. For example, if -a requires an argument, you would run ./script.sh -a argA -b.

Examples of getopts Command3

Silent Mode − If the first character of the optstring is a colon (:), getopts enters silent mode. In this mode, error messages are not printed, and the behavior changes in certain scenarios; for example, consider the optstring :a in the following case −

#!/bin/bash

while getopts ":a:" opt; do
   case $opt in
      a) echo "Option -a with argument $OPTARG";;
      :) echo "Option -$OPTARG requires an argument.";;
      \?) echo "Invalid option: -$OPTARG";;
   esac
done

When you run the script with a missing argument for -a, you will get the following output −

Examples of getopts Command4

If you run a script by adding a flag not present in the Optstring, you get encounter the following error −

By following these steps, you can see how the getopts command processes and handles different options and arguments in your script. This makes your script more versatile and user-friendly.

Conclusion

The getopts is a powerful and versatile command that is used in shell scripts to parse command-line options and arguments directly from the terminal.

In this tutorial, we explored the syntax of getopts command along with various options that can be used with it. Further, we also discussed the usage of getopts command to help you in gaining a good understanding of the command.

Advertisements