Why does this option only work the first time it's used, then ignored every other time? It's like it's being reset when the option is not used.
Here's my function:
testopts() {
local var="o false"
while getopts "o" option; do
case "${option}" in
o)
var="o true"
;;
esac
done
echo $var
}
When running it, it only returns true when passing the option for the first time.
$ testopts o false $ testopts -o o true $ testopts -o o false
If the option is expecting an argument, getopts gets that argument, and places it in $OPTARG. If an expected argument is not found, the variable optname is set to a colon (":"). Getopts then increments the positional index, $OPTIND, that indicates the next option to be processed.
optstring must contain the option letters the command using getopts recognizes. If a letter is followed by a colon, the option is expected to have an argument, or group of arguments, which must be separated from it by white space.
Option letters are case sensitive.
Description. The getopts command is a Korn/POSIX Shell built-in command that retrieves options and option-arguments from a list of parameters. An option begins with a + (plus sign) or a - (minus sign) followed by a character. An option that does not begin with either a + or a - ends the OptionString.
You need to add this line at top of your function:
OPTIND=1
Otherwise successive invocation of the function in shell are not resetting this back since function is being run in the same shell every time.
As per help getopts
:
Each time it is invoked,
getopts
will place the next option in the shell variable$name
, initializing name if it does not exist, and the index of the next argument to be processed into the shell variableOPTIND
.OPTIND
is initialized to1
each time the shell or a shell script is invoked.
Resetting OPTIND
to 1
works, but it's even better to declare OPTIND
as a local variable whenever getopts
is used in a function.
See https://eklitzke.org/using-local-optind-with-bash-getopts
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With