help-bash
[Top][All Lists]
Advanced

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

Re: Why doesn't $? catch the exit status on interrupt?


From: Kerin Millar
Subject: Re: Why doesn't $? catch the exit status on interrupt?
Date: Thu, 28 Jul 2022 16:21:52 +0100

Hi Masahiro,

On Thu, 28 Jul 2022 23:30:18 +0900
Masahiro Yamada <masahiroy@kernel.org> wrote:

> So, is there any way to know that it was interrupted?
> 
> 
> More experiments.
> 
> $ bash -c 'trap "echo status: \$?" EXIT; true; sleep 3'
> ^Cstatus: 0
> 
> $ bash -c 'trap "echo status: \$?" EXIT; false; sleep 3'
> ^Cstatus: 1
> 
> 
> So, if I interrupt 'sleep 3' by pressing Ctrl-C,
> $? contains the exit status of the command running before.
> (In this case, the exit status of 'true' or 'false')
> 
> 
> 
> In contrast, if I do something similar in an interactive shell,
> $? contains 130.
> 
> 
> $ false
> $ sleep 3
> ^C
> $ echo $?
> 130
> 
> 
> 
> How can I get 130 in script mode?

Enabling job control with set -m should produce the equivalent behaviour. 
Otherwise, I think that you would either have to refrain from setting an EXIT 
trap or supplement it with an INT trap. For example, trap 'trap - INT; kill -s 
INT $$' INT.

-- 
Kerin Millar



reply via email to

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