I defined the function f
in Bash based on the example here (under "An option with an argument"):
f () {
while getopts ":a:" opt; do
case $opt in
a)
echo "-a was triggered, Parameter: $OPTARG" >&2
;;
\?)
echo "Invalid option: -$OPTARG" >&2
return 1
;;
:)
echo "Option -$OPTARG requires an argument." >&2
return 1
;;
esac
done
}
Whereas they use a script, I directly define the function in the shell.
When I first launch Bash and define the function, everything works: f -a 123
prints -a was triggered, Parameter: 123
. But when I run the exact same line a second time, nothing is printed.
What's causing this behavior? It happens in Bash 3.2 and 4.3, but it works fine in Zsh 5.1. This is surprising because the example was supposed to be for Bash, not for Zsh.
unset opt OPTARG OPTIND
before everywhile getopts...
call and it now works flawlessly. Thanks :) – Deep Oct 06 '18 at 09:41