How to support both short and long options at the same time in bash? [duplicate] How to support both short and long options at the same time in bash? [duplicate] bash bash

How to support both short and long options at the same time in bash? [duplicate]


getopt supports long options.

http://man7.org/linux/man-pages/man1/getopt.1.html

Here is an example using your arguments:

#!/bin/bashOPTS=`getopt -o axby -l long-key: -- "$@"`if [ $? != 0 ]then    exit 1fieval 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;;    esacdoneecho "Args:"for argdo    echo $argdone

Output of $ foo -ax --long-key val -b -y SOME FILE NAMES:

Got aGot xGot long-key, arg: valGot bGot yArgs:SOMEFILENAMES