Hey is there any difference between $OPTIND and $#?
Is there a certain reason for that you use $OPTIND with getopts, not $#?

  • Assume you run your program with "myprogram -a -b -c one two three". How do you split out "one", "two", and "three"?
    – Mikel
    Oct 23, 2015 at 21:15

1 Answer 1


$OPTIND indicates how far you have progressed through parsing the parameter list (i.e., for options), while $# is simply the number of parameters. They are not really related, because $OPTIND changes, while $# does not (unless you use shift).

The POSIX description of getopts goes into some detail.

  • When I used shift $((OPTIND-1), then $OPTIND didn't changed but $# changed. That made the difference clear for me. Thank you ! :) Oct 23, 2015 at 22:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.