2

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

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

1 Answer 1

5

$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.

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

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.