[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: Why doesn't $? catch the exit status on interrupt?
From: |
Masahiro Yamada |
Subject: |
Re: Why doesn't $? catch the exit status on interrupt? |
Date: |
Fri, 29 Jul 2022 16:06:38 +0900 |
On Fri, Jul 29, 2022 at 12:22 AM Kerin Millar <kfm@plushkava.net> wrote:
>
> 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.
Thanks.
'set -m' seems to work for me.
I do not know if there is any drawback in using -m
in the script mode.
With a quick test, I do not see any issue in 'set -m'
--
Best Regards
Masahiro Yamada