Skip to content
Tutorials Center
My learning

Topics

Linux Command Reference ยท Shell Builtins

Linux getopts command

Reference for the Bash getopts builtin: parse short options with and without arguments, handle errors, use OPTIND and OPTARG, and shift away the options.

getopts is the standard builtin for parsing short command-line options such as -v, -n 5 and -vn5 in a shell script. Each call looks at the next option, stores its letter in a variable you name, and stores the option's argument (if any) in OPTARG. It fits naturally in a while loop that ends when the options run out.

It handles bundled flags (-vq), attached values (-n5), and the -- end-of-options marker. It does not understand long options such as --verbose; use a case loop over $1 for those, or the external getopt program.

Terminal
while getopts "vn:" opt; do
  case $opt in
    v) verbose=1 ;;
    n) count=$OPTARG ;;
  esac
done
shift $((OPTIND - 1))

Try it

Bash A typical option loop

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.

Bash Silent mode and error messages

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.

Syntax

Terminal
getopts OPTSTRING NAME [ARG ...]
Name Description
OPTSTRING The accepted option letters. A letter followed by : takes an argument. A leading : selects silent error handling.
NAME Variable that receives the current option letter.
ARG ... Optional words to parse instead of the positional parameters.

Variables used: OPTARG (the option's value, or the offending letter in silent mode), OPTIND (index of the next word to process, starting at 1), OPTERR (set to 0 to stop automatic messages).

Returns: 0 while an option was found, non-zero at the end of the options (first non-option word, --, or no more arguments).

Edge cases: an unknown option stores ? in NAME; a missing argument stores ? (normal mode) or : (silent mode).

Usage notes

  • Reset OPTIND. It starts at 1 once per shell, so a function that calls getopts twice must set OPTIND=1 first, or the second call sees leftover state. Make it local OPTIND in functions.
  • Shift afterwards. shift $((OPTIND - 1)) removes the parsed options and leaves the remaining operands in $@.
  • Options come first. getopts stops at the first word that does not begin with -, so script file -v leaves -v unparsed. This matches POSIX behavior.
  • Argument handling. -n5 and -n 5 both set OPTARG=5. A value that starts with - is accepted as a value (-n -3 gives -3).
  • Silent mode is friendlier. Start the string with a colon and print your own usage message in the \? and : branches.
  • No long options. For --help or --output=file use a manual while [ $# -gt 0 ] loop with case, or GNU getopt.
  • Not getopt. getopts is a shell builtin and portable; getopt is an external program with a different syntax and different quoting rules.
  • Use case with \?. The question mark must be escaped, or it matches any single character.