help-bash
[Top][All Lists]
Advanced

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

The EXIT trap; is it executed on abnormal shell exits?


From: Stan Marsh
Subject: The EXIT trap; is it executed on abnormal shell exits?
Date: Mon, 21 Sep 2026 04:08:06 -0600

The question is: Under what circumstances is an EXIT trap not executed?

There are basically two (as far as I know) problematic cases:

    1) When the shell exits via receipt of a signal (including
    keyboard generated signals - INT or QUIT).

    2) When the shell exits via "exec".

My memory is that it used to be true that either of these caused the
EXIT trap to not be executed, but things seem to have improved lately.

Recent testing shows that signals now seem to be trapped by the shell
and the EXIT trap gets executed.   I tested with all of signals 1, 7,
11, and 15, as well as by keyboarding INT and QUIT.  In all cases,
the EXIT traps gets run before the shell exits.  So, I conclude that
the shell must be setting up a handler for all of these signals, but I
am open to hearing about some other implementation detail.

However, "exec" still seems to be a problem.  If the shell exits via,
e.g., "exec sleep 5", then the EXIT trap is ignored.  Clear enough why
this happens, but it seems like it *could* be fixed.  Is that possible?

And just in case it is unclear, my overall goal is to be able to
ensure that regardless of how or why a shell script stops running,
that the EXIT trap that I have set in that script gets run.

=================================================================================
Please do not send me replies to my posts on the list.
I always read the replies via the web archive, so CC'ing to me is unnecessary.

When responding to my posts, please try to refrain from giving bureaucratic
answers.  If you have nothing useful to say, then just click Next and go on.

BTW, ain't interested in kludgey workarounds - got enough of my own!
(YT id: 6H1lcubN0oE)



reply via email to

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