#!/bin/bash -e
ARGUMENT_LIST=(
"arg-one"
"arg-two"
"arg-three"
)
# read arguments
opts=$(getopt \
--longoptions "$(printf "%s:," "${ARGUMENT_LIST[@]}")" \
--name "$(basename "$0")" \
--options "" \
-- "$@"
)
eval set --$opts
while [[ $# -gt 0 ]]; do
case "$1" in
--arg-one)
argOne=$2
shift 2
;;
--arg-two)
argTwo=$2
shift 2
;;
--arg-three)
argThree=$2
shift 2
;;
*)
break
;;
esac
done
Note: The eval
in eval set --$opts
is required as arguments returned by getopt
are quoted.
$ ./getopt.sh --arg-one "apple" --arg-two "orange" --arg-three "banana"
https://medium.com/@codebyamir/parse-command-line-arguments-using-getopt-24a90b5b317d