1

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 '15 at 21:15
4

$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 ! :) – micholeodon Oct 23 '15 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.