NetBSD-Bugs archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

Re: bin/37493: /bin/sh exits if shift fails, but shouldn't



On Thu, Dec 06, 2007 at 09:55:01PM +0000, rillig%NetBSD.org@localhost wrote:
 > When "shift" is used in "set -e" mode, the shell exits if the "shift"
 > occurs in a conditional statement. This is unexpected and not covered by
 > the documentation.
 > 
 > 
 > >How-To-Repeat:
 > $ sh -c 'set -ex; shift || echo failed.'
 > + shift
 > shift: can't shift that many

So, the issue is that on line 1039 of eval.c there's a test that
re-raises the exception (EXERROR, aka "error") if the command was a
special builtin. This gets caught in main() and results in _exit(2),
whether or not -e is used; a bad shift is immediately and
unconditionally fatal.

Commenting that test out yields the expected behavior.

However, commenting it out clearly is the wrong thing. It would make
things like sh -c 'set -Z || echo failed' not bail out, which would be
wrong, or at least inconsistent with ksh/bash/zsh. (-Z is an illegal
option.)

I'm thinking that shift should be changed to an ordinary builtin
instead of a special builtin. But I'm not sure what other implications
this might have. Would someone who understands the finer points of sh
like to comment?

Also... ksh exhibits the same behavior. So, apparently, does the
original Bourne shell. bash and zsh do not. Which makes it unclear if
it's really a bug or not, too...

-- 
David A. Holland
dholland%netbsd.org@localhost


Home | Main Index | Thread Index | Old Index