bug-bash
[Top][All Lists]
Advanced

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

Re: $? is -1


From: Mike Jonkmans
Subject: Re: $? is -1
Date: Sat, 6 Jul 2019 00:35:28 +0200

On Fri, Jul 05, 2019 at 07:03:16PM +0100, Stephane Chazelas wrote:
> Date: Fri, 5 Jul 2019 19:03:16 +0100
> From: Stephane Chazelas <address@hidden>
> To: Chet Ramey <address@hidden>
> Cc: address@hidden, address@hidden, address@hidden
> Subject: Re: $? is -1
> 
> 2019-07-05 09:03:06 -0400, Chet Ramey:
> [...]
> > >   Second, when ran in a subshell, the same exit status gets mapped to 255.
> > >   While logical, as -1 gets mapped to 255, it seems inconsistent.
> > >   ( from the manual: "The return status is the exit status of list." )
> > 
> > It's the difference between passing a status around the shell and passing
> > it through the waitpid() interface, which takes just eight bits.
That is true, but the manual states that the return status is the exit status 
of the list.
But the exit status (-1) and the return status (255) are not equal.
> [...]
> 
> Note though:
> 
> $ bash -c 'f() { return "$1"; }; f -1; echo "$?"'
> 255
> 
> It gets mapped to 255 even though there's no waitpid().

This is a funny one. So bash tries to keep $? between 0..255 in this case.

> bash also takes upon itself to truncate the number passed to
> exit(1) before passing it to _exit(2):
> 
> $ strace -fe exit_group bash -c 'exit -1'
> exit_group(255)                         = ?
> 
> There's a lot of variation between shells (and other utilities)
> on that front. See also:
> 
> https://unix.stackexchange.com/questions/418784/what-is-the-min-and-max-values-of-exit-codes-in-linux/418802#418802
I had found that too; it says Posix doesn't specify behaviour of builtin exit 
called with values outside 0..255 
And that bash truncates to 8 bit.
Posix probably doesn't specify the behaviour because there is no common ground 
between shells.

I would suggest bash always has its $? values between 0..255 (or at least 
non-negative)

> https://unix.stackexchange.com/questions/99112/default-exit-code-when-process-is-terminated/99134#99134

Missed that one. Thanks.
> 
> -- 
> Stephane

-- 
Mike Jonkmans <address@hidden>



reply via email to

[Prev in Thread] Current Thread [Next in Thread]