#!/bin/bash # Script from # http://www.unixboard.de/vb3/showthread.php?36246-getopts-ordentliche-Parameternamen&p=285798&viewfull=1#post285798 # Optimized: # - $restargs works now (+ unquoted) # Still not working: # - Optional arguments (::) don't work with long options. For short options, no whitespace is allowed optarr=( $(getopt --options 'ab:c::' --long 'a-long,b-long:,c-long::' --unquoted -- "$@") ) i=0 while true; do case ${optarr[$i]} in -a|--a-long) echo "Option a" ; ((i++));; -b|--b-long) echo "Option b, argument ${optarr[$((i+1))]}";((i=i+2));; -c|--c-long) # c has an optional argument. As we are in quoted mode, # an empty parameter will be generated if its optional # argument is not found. case "${optarr[$((i+1))]}" in "''") echo "Option c, no argument"; ((i=i+2));; *) echo "Option c, argument is ${optarr[$((i+1))]}"; ((i=i+2));; esac;; --) ((i++)); break;; *) echo "Internal Error!" >&2; exit 1;; esac done restargs=${optarr[@]:i}; echo "Restargs:" $restargs;