getopts for Parsing Command-Line Flags
In this page:
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
#!/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
Login to try C/C++/Java/PHP code in the editor
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
#!/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"
Login to try C/C++/Java/PHP code in the editor
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
#!/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
Login to try C/C++/Java/PHP code in the editor
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
#!/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: $*"
Login to try C/C++/Java/PHP code in the editor
- 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. - Not resetting
OPTINDto 1 ifgetoptsis used more than once in the same shell session (e.g. across multiple function calls), causing later calls to skip arguments. - Forgetting a colon after an option letter that takes an argument (like
f:), which makes getopts think-ftakes no argument.
getopts "optstring" nameparses 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.
Chapter Quiz — Complete all 7 topics to unlock
0/7 topics done
Complete these topics first:
- getopts for Parsing Command-Line Flags
- Positional Parameters and shift
- Process Substitution in Practice (diff <(...) <(...))
- Arrays of Arguments ("$@" vs "$*" Quoting Difference)
- Nameref Variables (declare -n) for Passing Arrays by Reference
- Heredoc Templating (Generating a Config File from Variables)
- Default/Fallback Argument Handling