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.
while getopts "vn:" opt; do case $opt in v) verbose=1 ;; n) count=$OPTARG ;; esac done shift $((OPTIND - 1))
Try it
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.
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
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 at1once per shell, so a function that callsgetoptstwice must setOPTIND=1first, or the second call sees leftover state. Make itlocal OPTINDin functions. - Shift afterwards.
shift $((OPTIND - 1))removes the parsed options and leaves the remaining operands in$@. - Options come first.
getoptsstops at the first word that does not begin with-, soscript file -vleaves-vunparsed. This matches POSIX behavior. - Argument handling.
-n5and-n 5both setOPTARG=5. A value that starts with-is accepted as a value (-n -3gives-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
--helpor--output=fileuse a manualwhile [ $# -gt 0 ]loop withcase, or GNUgetopt. - Not
getopt.getoptsis a shell builtin and portable;getoptis an external program with a different syntax and different quoting rules. - Use
casewith\?. The question mark must be escaped, or it matches any single character.
Related
shift,read,set, Special parameters,case- Lessons: Script arguments, Script design, Case and select