getopts

The builtin that parses short command-line options such as -v, -vq and -f value, one option per call, in a while loop.

In more detail

Each call sets a variable to the option letter and OPTARG to its value, and a case statement acts on it. getopts handles bundled flags and values for you, but only single-letter options, not long ones such as --verbose.

Example

while getopts ":vf:" opt; do case "$opt" in v) verbose=1 ;; f) file="$OPTARG" ;; esac; done

Related terms

Taught in

References