Linux getopt command is often used to parse script options. I find a piece of boilerplate code for this command and mark it in this post for further reference.
set -eu set +e options=$(getopt --name "demo.sh" -o "f:d" -l "file:,debug" -- "$@") if [ "$?" -ne 0 ]; then print_script_usage exit 1 fi set -e eval set -- "${options}" while [[ "${#@}" -gt 0 ]]; do case "$1" in -f | --file ) shift file=$1 ;; -d | --debug ) debug=1 ;; --) shift ;; *) print_script_usage exit 1 ;; esac shift done
-o is used to specify the short options, while -l is used to specify the long options. A colon following the option name indicates that the option requires a option argument.