Parse Command Line Arguments in Bash on Linux


Abstract

Command-line arguments can be entered sequentially or processed into options by bash programs. These arguments are used by command-line utilities to selectively select between execution environments or conditionally trigger functions in a Bash script. They can be set up in various ways in Bash.

Note − Linux commands are case-sensitive.

getopt Syntax

The Syntax for getopts is −

$ getopts optstring opt [arg ...]

The following applies to the aforementioned function −

  • The options are represented by an optstring. If there is a colon (:) following the option, it expects a response. In the optstring, option c, for example, would be denoted as c: if it expected an argument.

  • When an option has a corresponding argument, getopts stores the string value of the argument in the OPTARG shell variable. The OPTARG variable, for instance, would hold the argument given to option c.

We shall see these ideas in action in the parts that follow and they will become evident.

Parsing Complex Arguments using getopt

When the number of arguments increases or when the assignment of the variables is subject to a condition. In these situations, a strong structure must be in place. This issue is resolved by the command-line tool getopt, which offers syntax and options to define and parse the parameters.

This is a quick guide on utilizing the getopt to define arguments.

When supplying arguments to a command-line utility, there are two types. They consist of −

  • Short Arguments: A hyphen and one character are used to define these. Examples are -h for help and -l for a list command.

  • Long Arguments: These are complete strings that have two hyphens before them. For instance, —help and —list indicate help and list respectively.

Consider this script tutorials_options.sh, where set up arguments with the getopt utility −

#!/bin/bash

SHORT=p:,q:,r
LONG=tutorial1:, tutorial2:, help
OPTS=$(getopt --alternative --name class --options $SHORT --longoptions $LONG -- "$@") 

The getopt utility's —options flag receives the short arguments, whereas the —longoptions flag receives the long arguments. We have three condensed possibilities in the code above −

  • P stands for tutorial 1

  • Q for tutorial 2

  • R stands for help

Consider the shift command used by the tutorials_test.sh script to record and report the values of the options in our arguments −

#!/bin/bash

SHORT=p:,:,r
LONG=tutorial1:,tutorial2:,help
OPTS=$(getopt -a -n class --options $SHORT --longoptions $LONG -- "$@")

eval set -- "$OPTS"

while :
do
  case "$1" in
    -p | --tutorial1 )
      tutorial1="$2"
      shift 2
      ;;
    -q | --tutorial2 )
      tutorial2="$2"
      shift 2
      ;;
    -r | --help)
      "This is a class script"
      exit 2
      ;;
    --)
      shift;
      break
      ;;
    *)
      echo "Unexpected option: $1"
      ;;
  esac
done

echo $tutorial1, $tutorial2
) 

The two assigned variables are printed as follows when the script is invoked with the proper arguments −

$ bash tutorials_test.sh --tutorial1 class1 --tutorial2  class2
class1, class2 

Take note of how the script terminated when it came across a double hyphen (--).

Conclusion

The fundamentals of passing positional parameters from a shell script and parsing complex optional arguments have been discussed in this article.

These real-world examples can be used to strengthen our shell scripts in daily life.

Updated on: 23-Mar-2023

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements