Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > gnu.bash.bug > #14256

Re: $OPTIND varibale value is different from sh

From Ilkka Virta <itvirta@iki.fi>
Newsgroups gnu.bash.bug
Subject Re: $OPTIND varibale value is different from sh
Date 2018-06-20 19:45 +0300
Message-ID <mailman.2306.1529513185.1292.bug-bash@gnu.org> (permalink)
References <ea25c4c012ab73dff24d28714d4bd8@cweb13.nm.nhnsystem.com> <20180620123950.i6etdbcjgodbfse6@eeg.ccf.org>

Show all headers | View raw


On 20.6. 15:39, Greg Wooledge wrote:
> On Wed, Jun 20, 2018 at 05:16:48PM +0900, Hyunho Cho wrote:
>> set -- -a -bc hello world
>> getopts abc opt "$@"
>> getopts abc opt "$@"          # bash  = b, 2                  <------ different from "sh"
>> echo $opt, $OPTIND            # sh      = b, 3

> Since POSIX doesn't say anything about what OPTIND should contain
> when you're in the middle of processing an argument with multiple
> concatenated options inside it, I'd say that this falls into the
> realm of "unspecified behavior", and shells can do anything they
> want.  So, not a bug in bash.  Or in dash.  Or in ksh, which by the
> way does the same thing as bash.

Even worse, OPTIND doesn't contain all the necessary information. It 
just points at an argument, but doesn't tell what character within it is 
being processed (next). Resetting OPTIND to 1 is valid, so assigning to 
it has to clear that hidden information, so a simple  OPTIND=$OPTIND 
can have some curious effects...


I tested a couple of shells out of curiosity. Zsh in particular has a 
yet another set of values it returns in OPTIND during the loop. Though 
in the end, the final value is correct in all shells I tried:

$ for sh in dash 'busybox sh' bash ksh93 zsh ; do printf "%-10s: " 
"$sh"; $sh -c 'while getopts abcd opt; do printf "$OPTIND  "; done; 
printf "$OPTIND  "; shift $(($OPTIND - 1)); echo "$1" ' sh -a -bcd 
hello; done
dash      : 2  3  3  3  3  hello
busybox sh: 2  3  3  3  3  hello
bash      : 2  2  2  3  3  hello
ksh93     : 2  2  2  3  3  hello
zsh       : 1  2  2  2  3  hello


-- 
Ilkka Virta / itvirta@iki.fi

Back to gnu.bash.bug | Previous | Next | Find similar | Unroll thread


Thread

Re: $OPTIND varibale value is different from sh Ilkka Virta <itvirta@iki.fi> - 2018-06-20 19:45 +0300

csiph-web