How does the OPTIND variable work in the shell builtin getopts How does the OPTIND variable work in the shell builtin getopts bash bash

How does the OPTIND variable work in the shell builtin getopts


According to man getopts, OPTIND is the index of the next argument to be processed (starting index is 1). Hence,

In sh foo.sh -abc CCC Blank arg1 is -abc, so after a we are still parsing arg1 when next is b (a 1). Same is true when next is c, we are still in arg1 (b 1). When we are at c, since c needs an argument (CCC) the OPTIND is 3 (arg2 is CCC and we skip it).

In sh foo.sh -a -b -c CCC Blank, arg1 is a, arg2 is b, arg3 is c, and arg4 is CCC. So we get a 2, b 3, c 5.

In sh foo.sh -ab -c CCC Blank args are (1:-ab, 2: -c, 3: CCC and 4: Blank). So we get: a 1, b 2, c 4.

In sh foo.sh -a -bc CCC Blank args are (1: -a, 2: -bc, 3: CCC, 4: Blank) and we get a 2, b 2, c 4.