help-bash
[Top][All Lists]
Advanced

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

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


From: Masahiro Yamada
Subject: Why doesn't $? catch the exit status on interrupt?
Date: Thu, 28 Jul 2022 16:55:23 +0900

Hi.

I want to do something in the EXIT trap
when a command fails (including interruption).


If I pass the code to "bash -c", it works as I expect.
(If I press Ctrl-C, I see 130).

$ bash -c "trap 'echo status: $?' EXIT; sleep 3"
^Cstatus: 130




However, if I do the same thing in a script, it does not work.

$ cat test.sh
#!/bin/bash
trap 'echo status: $?' EXIT
sleep 3
$ ./test.sh
^Cstatus: 0




I think I am terribly missing something,
but why does the latter show 0 instead of 130?


-- 
Best Regards
Masahiro Yamada



reply via email to

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