# getopts in BASH

Using options with a BASH script is pretty annoying. I think the simplest route is to use getopts, which is a builtin BASH command. There are nicer ones, but then you need to install them. Here’s a bare-bones script (based on a script here):

Then we can process our actual arguments. Here’s an example:

To do better error handling, we can add : to the beginning of the $OPTSTR, and then $FLAG will be \? for bad arguments: