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


Groups > gnu.bash.bug > #15082 > unrolled thread

Re: $? is -1

Started byMike Jonkmans <bashbug@jonkmans.nl>
First post2019-07-06 00:35 +0200
Last post2019-07-06 00:35 +0200
Articles 1 — 1 participant

Back to article view | Back to gnu.bash.bug

This discussion starts older than the indexed window; earlier articles aren't shown. The article labeled Started by below is the oldest one visible, not the original post.


Contents

  Re: $? is -1 Mike Jonkmans <bashbug@jonkmans.nl> - 2019-07-06 00:35 +0200

#15082 — Re: $? is -1

FromMike Jonkmans <bashbug@jonkmans.nl>
Date2019-07-06 00:35 +0200
SubjectRe: $? is -1
Message-ID<mailman.263.1562366136.2688.bug-bash@gnu.org>
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 <stephane.chazelas@gmail.com>
> To: Chet Ramey <chet.ramey@case.edu>
> Cc: bashbug@jonkmans.nl, bug-bash@gnu.org, bash@packages.debian.org
> 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 <bashbug@jonkmans.nl>

[toc] | [standalone]


Back to top | Article view | gnu.bash.bug


csiph-web