--- Cut Here---
origdir $ trap "cd /tmp;echo;echo INT" INT
origdir $ trap
trap -- 'cd /tmp;echo;echo INT' SIGINT
trap -- 'echo "Status: $?"' ERR
origdir $ ^C
INT
/tmp $ pwd
/tmp
/tmp $ cd -
/home/user/origdir
origdir $ cat
^C
Status: 130
origdir $ cat; echo "Got to here, status = $?"
^C
Status: 130
Got to here, status = 130
origdir $ trap INT
origdir $ cat; echo "Got to here, status = $?"
^CStatus: 130
origdir $
--- Cut Here---
1) Now, this is not an X-Y problem (*), but this (what is shown above) is
a "at the command line" version of a problem that showed up writing a
shell function.
Also note that I think the official position is going to be that this
is not a bug, but is rather a "known feature". I.e., something that
can't/won't be fixed.
2) I do a lot of my bash scripting in the form of shell functions,
which serve as replacements for aliases (which don't work very well in
any shell). I realize that the basic problem (explained below) would
go away if I switched it to a regular executable script, but I prefer
not to go that route. For various aesthetic and convenience reasons,
it will stay a function.
Now, the problem statement. In a nutshell, it is this: When you have
a trap on INT, and then run an external command (e.g., "cat"), when
you ^C during the external command execution, the command exits with
status 130, but does *not* run the trap command(s) and it also does
*not* abort the command line or enclosing shell function.
Note (for clarity):
1) Originally tested on bash 5.2.15. Retested on 5.3rc2, with
same results.
2) If there is no trap on INT, then ^C aborts both the external
command and the enclosing command line (or shell function), as
seen in the last couple of lines above.
So, the funny thing is that having any trap set at all causes the
external command to simply exit with status 130 without actually
running the trap.
3) If you are sitting at the bash prompt or are running an
internal command (e.g., read or select), and hit ^C, it works as
expected. That is, the trap is run, the current directory is
changed, the message is echo'd and all is well.
So, my questions are "Why?" and "Can it be made to work for externals
like it does for internals?"
=================================================================================
(*) Do I need to put a line in my sig block to this effect (for all
subsequent posts) ?
=================================================================================
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.