I'm using a variation of the BashFAQ manual loop and I want to convert it to a function and assign the arguments to local variable but I can't figure out the correct syntax. Here's what I have:
function parseArguments() {
local arguments=( "$@" )
while :; do
case ${1:-} in
-d|--debug)
set -o xtrace
# [...more stuff...]
;;
-p|--prompt)
IsPromptEnabled=true
;;
--)
shift
break
;;
-?*)
error ${LINENO} "\"${1:-}\" is an unknown option" 1
;;
*)
break
esac
shift
done
}
parseArguments "$@"
This works fine as is until I try to replace $1 in the loop with the value from arguments. ${arguments[0]} and every other variation I can think of fails, I'd like to understand why (and figure out the solution).
shift, and this command affects (modifies)$1. Hence if you just replace$1by a different variable, you break the semantics of your program.