title: How does the OPTIND variable work in the shell builtin getopts
body: <p>My shell script is quite simple, as the following:</p>
  
  <pre><code>  while getopts "abc:" flag; do
           echo "$flag" $OPTIND $OPTARG
    done
  </code></pre>
  
  <p>And I do some testing as the following:</p>
  
  <pre><code>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
  </code></pre>
  
  <p>I cannot understand how <code>OPTIND</code> works with different command line invocation, I am confused by the output.</p>
  
  <p>Can you help to figure out the mechanism of computing <code>OPTIND</code>?</p>