I followed the posts on stackexchange websites to parse command line arguments. My program only parses long arguments and all arguments are mandatory. Here is what I have done:
getopt --test > /dev/null if [[ $? -ne 4 ]]; then echo "getopt --test failed in this environment." exit 1 fi function quit { echo "$1" exit 1 } # Option strings LONG="producer-dir:,consumer-dir:,username:,password:,url:,kafka-host:,checkpoint-dir:" # read the options OPTS=$(getopt --longoptions ${LONG} --name "$0" -- "$@") if [ $? != 0 ]; then quit "Failed to parse options...exiting." fi eval set -- "$OPTS" # extract options and their arguments into variables. while true ; do case "$1" in --producer-dir ) PRODUCER_DIR="$2" shift 2 ;; --consumer-dir ) CONSUMER_DIR="$2" shift 2 ;; --username ) USERNAME="$2" shift 2 ;; --password ) PASSWORD="$2" shift 2 ;; --url ) URL="$2" shift 2 ;; --kafka-host ) KAFKA_HOST="$2" shift 2 ;; --checkpoint-dir ) CHECKPOINT_DIR="$2" shift 2 ;; -- ) shift break ;; *) echo "Internal error!" exit 1 ;; esac done No matter in which order I pass the arguments, the first one is ignored and the result is empty. The rest of the arguments are parsed as expected. What am I missing?