On Wed, Aug 10, 2011 at 18:47, Stephane CHAZELAS <stephane_chaze...@yahoo.fr > wrote:
> 2011-08-10, 12:00(+02), Bernd Eggink: > [...] > > function f > > { > > local OPTIND=1 > > > > echo "\$1=$1" > > } > > > > while getopts "abcdefg" opt > > do > > echo "opt=$opt" > > f $opt > > done > > -------------------------------- > > > > Calling the sript like this works fine: > > script -a -b -c > > > > But calling it like this leads to an endless loop: > > script -abc > [...] > > However, this also loops endlessly. The reason is most likely that bash > > maintains an additional internal variable holding the index of the > > current character, relative to the current word. While this variable is > > not directly accessible by the user, it is set to 0 whenever OPTIND is > > assigned a value. > [...] > > That would be a bug in bash in my opinion. If OPTIND is marked > local to the function, it shouldn't affect the behavior of > parent contexts. > > Note that that bug is also in ksh93, pdksh, mksh and posh > (though slightly different in that one), but not in ash nor zsh. > Seems like ksh93 (tested with version 93u 2011-02-08) implicitly declares OPTIND and OPTARG in functions defined in the `function NAME {}' syntax and everything works fine. But if OPTIND or OPTARG are explicitly declared as local it may not work as expected. Wish Chet would consider fixing this problem in future bash releases. :) > Note that if you set OPTIND local, you probably want to do the > same for OPTARG (and maybe OPTERR). > > -- > Stephane > >