The definition of $OPTIND
in POSIX shell, bash are quite consistent and intuitive - it is the index of the next arg to be read. However, its behavior in zsh
is quite puzzling, and I can't find document for it.
Example:
# ./test.sh:
foo() {
while getopts "1:2:34:" flag; do
echo flag: $flag
echo arg: $OPTARG
echo ind: $OPTIND
done &&
echo .
}
foo -1 1 -2 2 -3 3 -4 4
Now execute:
>>> sh ./test.sh
flag: 1
arg: 1
ind: 3
flag: 2
arg: 2
ind: 5
flag: 3
arg:
ind: 6 <<<< EXPECTED - next arg should be $6
.
>>> bash ./test.sh
flag: 1
arg: 1
ind: 3
flag: 2
arg: 2
ind: 5
flag: 3
arg:
ind: 6 <<<< EXPECTED - next arg should be $6
.
>>> zsh ./test.sh
flag: 1
arg: 1
ind: 3
flag: 2
arg: 2
ind: 5
flag: 3
arg:
ind: 5 <<<<<< NOTICE HERE
.
This is tested on zsh 5.3.1 (amd64-portbld-freebsd11.0)