How does the OPTIND variable work in the shell builtin getopts

Blank picture Blank · Jan 10, 2013 · Viewed 17.7k times · Source

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?

Answer

perreal picture perreal · Jan 10, 2013

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.