My shell script is quite simple, as the following:
while getopts "abc:" flag; do
echo "$flag" $OPTIND $OPTARG
done
And I do some testing as the following:
Blank@Blank-PC:~/lab/shell/getopts_go$ sh foo.sh -abc CCC Blank
a 1
b 1
c 3 CCC
Blank@Blank-PC:~/lab/shell/getopts_go$ sh foo.sh -a -b -c CCC Blank
a 2
b 3
c 5 CCC
Blank@Blank-PC:~/lab/shell/getopts_go$ sh foo.sh -ab -c CCC Blank
a 1
b 2
c 4 CCC
Blank@Blank-PC:~/lab/shell/getopts_go$ sh foo.sh -a -bc CCC Blank
a 2
b 2
c 4 CCC
I cannot understand how OPTIND
works with different command line invocation, I am confused by the output.
Can you help to figure out the mechanism of computing OPTIND
?