This question already has an answer here:
-
*
/questions/402377/using-getopts-in-bash-shell-script-to-get-long-and-short-command-line-options
/questions/402377/using-getopts-in-bash-shell-script-to-get-long-and-short-command-line-options
28 answers
Answers
getopt supports long options.
http://linux.about.com/library/cmd/blcmdl1_getopt.htm http://linux.about.com/library/cmd/blcmdl1_getopt.htm
Here is an example using your arguments:
#!/bin/bash OPTS=`getopt -o axby -l long-key: -- "$@"` if [ $? != 0 ] then exit 1 fi eval set -- "$OPTS" while true ; do case "$1" in -a) echo "Got a"; shift;; -b) echo "Got b"; shift;; -x) echo "Got x"; shift;; -y) echo "Got y"; shift;; --long-key) echo "Got long-key, arg: $2"; shift 2;; --) shift; break;; esac done echo "Args:" for arg do echo $arg done
Output of $ foo -ax --long-key val -b -y SOME FILE NAMES:
Got a Got x Got long-key, arg: val Got b Got y Args: SOME FILE NAMES
Source
License : cc by-sa 3.0
http://stackoverflow.com/questions/4180880/how-to-support-both-short-and-long-options-at-the-same-time-in-bash