My getopts reference page

I don’t use bash getopts often enough to remember its syntax exactly. I can’t just say man getopts or info getopts to see its man page, and while this tutorial is good, it’s not as straightforward as I’d prefer. So here’s my reference page.The template.

usage() {
  echo "Usage: $0 [options] arguments"
while getopts "$optstring" opt; do
  case $opt in
      exit 1
shift $((OPTIND - 1))

The option string is a mash of all the single-letter options you want. If an option takes an argument, follow it with a colon, and OPTARG is set to the argument in the loop. If the option string starts with a colon, error messages are not emitted.

That’s it. For more, go to the tutorial linked above!

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s