dash.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* getopts appears to not be shifting $@ when consuming options
@ 2021-01-29 18:25 earnestly
  2021-01-29 20:15 ` Harald van Dijk
  2021-01-29 20:36 ` Jilles Tjoelker
  0 siblings, 2 replies; 8+ messages in thread
From: earnestly @ 2021-01-29 18:25 UTC (permalink / raw)
  To: dash

In this example dash will repeatedly append 'attr=foo' to the list of
parameters in an infinite loop:

    #!/bin/dash -x

    while getopts :a: arg -a foo -a bar; do
        case $arg in
            a) set -- "$@" attr="$OPTARG"
        esac
    done
    shift "$((OPTIND - 1))"

Instead I expected this to result in parameter list containing
'attr=foo' and 'attr=bar'.

This works in all shells I have been able to test with the exception of
busybox sh:

    * sh   (bash)
    * bash (All versions from 1.14 through 5.1.4)
    * mksh (MIRBSD KSH R59 2020/05/16)
    * ksh  (93u+)
    * zsh  (5.8)
    * zsh --emulate sh
    * heirloom-sh (bourne)

The only workaround I've found is to explicitly use `shift 2` in the a)
case and obviate the final shift using OPTIND.  This will unfortunately
break every other shell.

^ permalink raw reply	[flat|nested] 8+ messages in thread

end of thread, other threads:[~2021-01-30 16:22 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-01-29 18:25 getopts appears to not be shifting $@ when consuming options earnestly
2021-01-29 20:15 ` Harald van Dijk
2021-01-30 15:31   ` Harald van Dijk
2021-01-29 20:36 ` Jilles Tjoelker
2021-01-29 21:19   ` Harald van Dijk
2021-01-29 22:25   ` earnestly
2021-01-30  6:39     ` Vladimir N. Oleynik
2021-01-30  7:36     ` Vladimir N. Oleynik

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).