A common task in shell scripting is to parse command line arguments to your
script. Bash provides the
getopts built-in function to do just that. This
tutorial explains how to use the
getopts built-in function to parse arguments and options to a bash script.
getopts function takes three parameters. The first is a specification of
which options are valid, listed as a sequence of letters. For example, the
'ht' signifies that the options
-t are valid.
The second argument to
getopts is a variable that will be populated with the
option or argument to be processed next. In the following loop,
opt will hold
the value of the current option that has been parsed by
This example shows a few additional features of
getopts. First, if an invalid
option is provided, the option variable is assigned the value
?. You can catch
this case and provide an appropriate usage message to the user. Second, this
behaviour is only true when you prepend the list of valid options with
disable the default error handling of invalid options. It is recommended to
always disable the default error handling in your scripts.
The third argument to
getopts is the list of arguments and options to be
processed. When not provided, this defaults to the arguments and options
provided to the application (
$@). You can provide this third argument to use
getopts to parse any list of arguments and options you provide.
Shifting processed options
OPTIND holds the number of options parsed by the last call to
getopts. It is common practice to call the
shift command at the end of your
processing loop to remove options that have already been handled from
Parsing options with arguments
Options that themselves have arguments are signified with a
:. The argument to
an option is placed in the variable
OPTARG. In the following example, the
t takes an argument. When the argument is provided, we copy its value
to the variable
target. If no argument is provided
getopts will set
:. We can recognize this error condition by catching the
: case and printing
an appropriate error message.
An extended example – parsing nested arguments and options
Let’s walk through an extended example of processing a command that takes
options, has a sub-command, and whose sub-command takes an additional option
that has an argument. This is a mouthful so let’s break it down using an
example. Let’s say we are writing our own version of the
command. In this version you can call
-h option to display a help message.
We can use
getopts to parse the
-h option with the following
In it we catch invalid options with
shift all arguments that have
been processed with
shift $((OPTIND -1)).
Now let’s add the sub-command
install to our script.
install takes as an
argument the Python package to install.
install also takes an option,
-t takes as an argument the location to
install the package to relative to the current directory.
To process this line we must find the sub-command to execute. This value is the first argument to our script.
subcommand=$1 shift # Remove `pip` from the argument list
Now we can process the sub-command
install. In our example, the option
actually an option that follows the package argument so we begin by removing
install from the argument list and processing the remainder of the line.
After shifting the argument list we can process the remaining arguments as if
they are of the form
package -t src/lib. The
-t option takes an argument
itself. This argument will be stored in the variable
OPTARG and we save it to
target for further work.
Putting this all together, we end up with the following script that parses
arguments to our version of
pip and its sub-command
After processing the above sequence of commands, the variable
hold the package to install and the variable
target will hold the target to
install the package to. You can use this as a template for processing any set of
arguments and options to your scripts.