Bash - how to avoid command "eval set --" evaluating variables
As you note, eval
is evil -- and there's no need to use it here.
#!/bin/bash# make args an array, not a stringargs=( )# replace long argumentsfor arg; do case "$arg" in --help) args+=( -h ) ;; --host|-hS) args+=( -s ) ;; --cmd) args+=( -c ) ;; *) args+=( "$arg" ) ;; esacdoneprintf 'args before update : '; printf '%q ' "$@"; echoset -- "${args[@]}"printf 'args after update : '; printf '%q ' "$@"; echowhile getopts "hs:c:" OPTION; do : "$OPTION" "$OPTARG" echo "optarg : $OPTARG" case $OPTION in h) usage; exit 0;; s) servers_array+=("$OPTARG");; c) cmd="$OPTARG";; esacdone
That is to say: When building up a command line, append individual items to an array; you can then expand that array, quoted, without risking either evaluation or undesired behavior via effects of string-splitting, glob expansion, etc.