1

Some shells automatically resets the OPTIND variable used getopts when entering a function, some not.

For instance GNU bash 3.2.57(1) does not reset the OPTIND variable when it enters a function while zsh 5.0.5 does.

Which shells automatically resets OPTIND when entering a function? What is the behaviour recommended by POSIX?

  • Using `getopts` to operate over function arguments? Are you resetting `OPTIND` to 1 before trying to use getopts in the function? – Etan Reisner Apr 30 '15 at 14:38
  • Your problem might be variables locality. You must declare local `OPTIND` (if you're using `getopts` both in the script and the function). check this out: http://stackoverflow.com/q/16654607/3374591 – ArnonZ Apr 30 '15 at 14:42
  • Yes this is the intended use. I do not reset `OPTIND` before. That's interesting, it seems that zsh resets `OPTIND` when it enters the function while bash does not. At least for the versions mentioned. – Burton Tretter Apr 30 '15 at 14:46
  • And check out http://pubs.opengroup.org/onlinepubs/9699919799//utilities/getopts.html – Michael Jaros Apr 30 '15 at 20:30
  • @all Thank you, these are useful bits of information! – Burton Tretter May 26 '15 at 12:03

0 Answers0