Parse short options using the getopts builtin.
Section: Script Structure
Parse short options with getopts
bash
bash
while getopts ':f:o:v' opt; do
case $opt in
f) file=$OPTARG ;;
o) output=$OPTARG ;;
v) verbose=1 ;;
\?) echo "invalid option: -$OPTARG" ;;
esac
doneExplanation
Good for POSIX-style short flags in shell scripts.
Learn the surrounding workflow
Compare similar commands or jump into common fixes when this command is part of a bigger troubleshooting path.
Related commands
Same sheet · prioritizing Script Structure