← Back to Bash Course | Chapter 13: Advanced Scripting | Lesson 1 of 7

getopts for Parsing Command-Line Flags

getopts is a built-in tool that helps a script understand flags like -v or -f filename that someone passes in when running it.

A Basic Flag Loop

getopts "optstring" name is called repeatedly inside a while loop; each call parses the next flag from the positional parameters into the variable name (opt here), returning non-zero once all options have been consumed.

Example: A Basic Flag Loop

bash
#!/bin/bash
set -- -v -f myfile.txt

while getopts "vf:" opt; do
    case $opt in
        v) echo "verbose mode on" ;;
        f) echo "file argument: $OPTARG" ;;
    esac
done

Options That Require an Argument

A colon after a letter in the optstring, like f:, tells getopts that -f must be followed by a value, which getopts places into $OPTARG for that iteration of the loop.

Example: Options That Require an Argument

bash
#!/bin/bash
set -- -n "Ada" -c 3

while getopts "n:c:" opt; do
    case $opt in
        n) name=$OPTARG ;;
        c) count=$OPTARG ;;
    esac
done

echo "name=$name count=$count"

Handling Unknown and Missing Options

Prefixing the optstring with a colon (:vf:) switches getopts into silent error mode: an unrecognized flag sets opt to ?, and a flag missing its required argument sets opt to :, both of which the script can handle explicitly instead of getopts printing its own message.

Example: Handling Unknown and Missing Options

bash
#!/bin/bash
set -- -x

while getopts ":vf:" opt; do
    case $opt in
        v) echo "verbose" ;;
        f) echo "file: $OPTARG" ;;
        \?) echo "unknown option: -$OPTARG" ;;
        :) echo "option -$OPTARG requires an argument" ;;
    esac
done

Shifting Past Parsed Options

After the getopts loop finishes, OPTIND holds the index of the first non-option argument. shift $((OPTIND - 1)) removes all parsed flags, leaving $@ set to just the remaining positional arguments.

Example: Shifting Past Parsed Options

bash
#!/bin/bash
set -- -v file1.txt file2.txt

while getopts "v" opt; do
    case $opt in
        v) verbose=1 ;;
    esac
done
shift $((OPTIND - 1))

echo "remaining arguments: $*"
Common Mistakes
  1. Forgetting the leading colon in the option string (e.g. :vf:) disables getopts's default error messages in favor of manual handling via the ? and : cases.
  2. Not resetting OPTIND to 1 if getopts is used more than once in the same shell session (e.g. across multiple function calls), causing later calls to skip arguments.
  3. Forgetting a colon after an option letter that takes an argument (like f:), which makes getopts think -f takes no argument.
Chapter Summary
  • getopts "optstring" name parses flags one at a time from the positional parameters, in a loop.
  • A colon after a letter in the optstring (e.g. f:) means that option requires an argument, stored in $OPTARG.
  • A leading colon in the optstring enables silent error handling: ? for an unknown option, : for a missing required argument.
  • After the loop, shift $((OPTIND - 1)) removes the parsed options, leaving only the remaining positional arguments.

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.