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– Mikel2015-10-23 21:15:34 +00:00Commented Oct 23, 2015 at 21:15
Add a comment |
1 Answer
$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$OPTINDdidn't changed but$#changed. That made the difference clear for me. Thank you ! :)micholeodon– micholeodon2015-10-23 22:02:01 +00:00Commented Oct 23, 2015 at 22:02