Handling command line arguments in Bash

Learning resources

getopts handles flags/options/switchs with one dash ( - ), with or without an option argument/argument

It is a Bash builtin. It does not handle GNU-style long options (--myoption)

Notes

while getopts 'hu:' opt
do
   case $opt in
   h)  usage ;;
   u) value=$OPTARG;;
   esac
done

$OPTARG holds the value of the argument passed to the option if this option that requires an argument

Back to top Index