#1101388 dash: POSIX behavior / shift N calls exit if N > $# (positional arg count)

Package:
dash
Source:
dash
Description:
POSIX-compliant shell
Submitter:
Jari Aalto
Date:
2025-03-26 20:54:02 UTC
Severity:
normal
Tags:
#1101388#5
Date:
2025-03-26 20:35:05 UTC
From:
To:
Problem:

   set -- 1
   shift 2
   ... causes program to exit

The exit is a bit unexpected as the whole
program terminates. Suggested change:

   1. shift all positional parameters
   2. set status code $?
   3. continue execution

   This would also be allowed by POSIX:
   Bash and Zsh behaves like this.

This would make it possible to write code:

   set -- 1
   if shift 2 ; then
      ... do something, a warning message to user ...
   fi
   ... continue

Caveat: I'm aware of this, but perhaps
the default behaviour with N could be
chnaged to be more script friendly.

     shift $#

See also attached test script.

CONSEQUENCES OF ERRORS

Program execution is interrupted and
program cannot do any error handling of
their own.

Ref: POSIX 2018 (shift):

https://pubs.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html#tag_18_25_01

    EXIT STATUS

    If the n operand is invalid or is greater than "$#", this may be
    considered a syntax error and a non-interactive shell may exit; if
    the shell does not exit in this case, a non-zero exit status shall
    be returned. Otherwise, zero shall be returned.