#!/bin/bash shortopts=a:bc # iterate over options, breaking -ab into -a -b and --foo=bar into --foo bar # also turns -- into --endopts to avoid issues with things like wget -O- while (($#)); do case $1 in # if option is of type -ab -[!-]?*) OPTIND=1 while ((OPTIND==1)); do getopts "$shortopts" opt [[ $opt = "?" ]] && die options+=("-$opt" ${OPTARG+"$OPTARG"} ) done ((OPTIND > 2)) && shift $((OPTIND-2)) ;; # if option is of type --foo=bar, split on first '=' --?*=*) options+=("${1%%=*}" "${1#*=}");; # add --endopts for -- --) options+=(--endopts);; # otherwise, nothing special *) options+=("$1");; esac shift done # set new positional parameters to altered options set -- "${options[@]}" # actually parse the options and do stuff while [[ $1 = -?* ]]; do case $1 in -h|--help) usage; exit 0;; --endopts) shift; break;; *) die "invalid option: $1";; esac shift done