I'm trying to create a script that has an option that will contain arbitrary text (including spaces) surrounded by quotes and this is proving difficult to search for and implement.Basically the behavior I would like to have is docker_build_image.sh -i image -v 2.0 --options --build-arg ARG=value, this will be a helper script for simplifying versioning docker images with our build server.The closest I've come to successfully grabbing the --options value gives me an error from getopt, unrecognized option '--build-arg ARG=value'.The full script is below#!/usr/bin/env bashset -o errexit -o noclobber -o nounset -o pipefailparams=$(getopt -o hi:v: -l help,image:,options,output,version: --name $0 -- $@)eval set -- $paramsshow_help() {cat << EOFUsage: ${0##*/} [-i IMAGE] [-v VERSION] [OPTIONS...]Builds the docker image with the Dockerfile located in the current directory.    -i, --image         Required. Set the name of the image.    --options           Set the additional options to pass to the build command.    --output            (Default: stdout) Set the output file.    -v, --version       Required. Tag the image with the version.    -h, --help          Display this help and exit.EOF}while [[ $# -gt 0 ]]do    case $1 in        -h|-\?|--help)            show_help            exit 0            ;;        -i|--image)            if [ -n $2 ]; then                IMAGE=$2                shift            else                echo -e ERROR: '$1' requires an argument.\n >&2                exit 1            fi            ;;                -v|--version)                        if [ -n $2 ]; then                VERSION=$2                shift            else                echo -e ERROR: '$1' requires an argument.\n >&2                exit 1            fi            ;;        --options)        echo -e OPTIONS=$2\n            OPTIONS=$2        ;;        --output)                        if [ -n $2 ]; then                BUILD_OUTPUT=$2                shift            else                BUILD_OUTPUT=/dev/stderr            fi        ;;        --)            shift            break        ;;        *)            echo -e Error: $0 invalid option '$1'\nTry '$0 --help' for more information.\n >&2            exit 1        ;;    esacshiftdoneecho IMAGE: $IMAGEecho VERSION: $VERSIONecho # Grab the SHA-1 from the docker build outputID=$(docker build ${OPTIONS} -t ${IMAGE}  . | tee $BUILD_OUTPUT | tail -1 | sed 's/.*Successfully built \(.*\)$/\1/')# Tag our imagedocker tag ${ID} ${IMAGE}:${VERSION}docker tag ${ID} ${IMAGE}:latest
How to parse Command Line Arguments with arbitrary string
bash;shell script;getopts
Just handle it like you handle the others that take an argument (image and version). i.e. add the colon marking a mandatory argument to the option string that goes to getopt, and pick the value off of $2. I think the error you get comes from getopt, since it's not told that options takes an argument, and so it tries to interpret --build-arg ARG=value as a long option (it does start with a double-dash).$ cat opt.sh#!/bin/bashparams=$(getopt -o hv: -l help,options:,version: --name $0 -- $@)eval set -- $paramswhile [[ $# -gt 0 ]] ; do    case $1 in        -h|-\?|--help)            echo help            ;;        -v|--version)                        if [ -n $2 ]; then                echo version: <$2>                shift            fi            ;;        --options)                        if [ -n $2 ]; then                echo options: <$2>                shift            fi            ;;    esac    shiftdone$ bash opt.sh --version 123 --options blah --options foo barversion: <123>options: <blah>options: <foo bar>