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


Groups > gnu.bash.bug > #14258

Re: $OPTIND varibale value is different from sh

Path csiph.com!goblin3!goblin1!goblin.stu.neva.ru!usenet.stanford.edu!not-for-mail
From Martijn Dekker <martijn@inlv.org>
Newsgroups gnu.bash.bug
Subject Re: $OPTIND varibale value is different from sh
Date Wed, 20 Jun 2018 19:22:23 +0100
Lines 43
Approved bug-bash@gnu.org
Message-ID <mailman.2310.1529518950.1292.bug-bash@gnu.org> (permalink)
References <ea25c4c012ab73dff24d28714d4bd8@cweb13.nm.nhnsystem.com> <20180620123950.i6etdbcjgodbfse6@eeg.ccf.org> <b4167746-e795-274c-37a2-f77396d4f8af@iki.fi>
NNTP-Posting-Host lists.gnu.org
Mime-Version 1.0
Content-Type text/plain; charset=utf-8; format=flowed
Content-Transfer-Encoding quoted-printable
X-Trace usenet.stanford.edu 1529518951 18671 208.118.235.17 (20 Jun 2018 18:22:31 GMT)
X-Complaints-To action@cs.stanford.edu
To bug-bash@gnu.org
Envelope-to bug-bash@gnu.org
User-Agent Mozilla/5.0 (Macintosh; Intel Mac OS X 10.11; rv:52.0) Gecko/20100101 Thunderbird/52.7.0
In-Reply-To <b4167746-e795-274c-37a2-f77396d4f8af@iki.fi>
Content-Language en-GB
X-MIME-Autoconverted from 8bit to quoted-printable by kahlil.inlv.org id w5KIMMBq029323
X-detected-operating-system by eggs.gnu.org: GNU/Linux 3.x [fuzzy]
X-Received-From 37.59.109.123
X-BeenThere bug-bash@gnu.org
X-Mailman-Version 2.1.21
Precedence list
List-Id Bug reports for the GNU Bourne Again SHell <bug-bash.gnu.org>
List-Unsubscribe <https://lists.gnu.org/mailman/options/bug-bash>, <mailto:bug-bash-request@gnu.org?subject=unsubscribe>
List-Archive <http://lists.gnu.org/archive/html/bug-bash/>
List-Post <mailto:bug-bash@gnu.org>
List-Help <mailto:bug-bash-request@gnu.org?subject=help>
List-Subscribe <https://lists.gnu.org/mailman/listinfo/bug-bash>, <mailto:bug-bash-request@gnu.org?subject=subscribe>
Xref csiph.com gnu.bash.bug:14258

Show key headers only | View raw


Op 20-06-18 om 17:45 schreef Ilkka Virta:
> $ 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

yash has interesting behaviour here. While other shells use an extra 
internal (i.e. not exposed to the shell language) state variable to keep 
track of parsing multiple options combined in a single argument, yash 
just adds that extra state value to OPTIND.

yash      : 1:2  2:2  2:3  2:4  3  hello

Which demonstrates quite well that the value of OPTIND in the middle of 
processing really should be considered unspecified, and that only the 
initial value (1) and the final value can be relied upon.

Relevant POSIX text:

http://pubs.opengroup.org/onlinepubs/9699919799/utilities/getopts.html#tag_20_54
| If the application sets OPTIND to the value 1, a new set of
| parameters can be used: either the current positional parameters or
| new arg values. Any other attempt to invoke getopts multiple times in
| a single shell execution environment with parameters (positional
| parameters or arg operands) that are not the same in all invocations,
| or with an OPTIND value modified to be a value other than 1, produces
| unspecified results.


- M.

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


Thread

Re: $OPTIND varibale value is different from sh Martijn Dekker <martijn@inlv.org> - 2018-06-20 19:22 +0100

csiph-web