Take the following script, interrupted by keyboard input Ctrl+C as shown:
$ function a() { echo "Performing"; sleep 10; echo "Performed"; }
$ a
Performing
^C
$ echo "${FUNCNAME[@]}"
a source
If we repeat the SIGINT during function a many times, we eventually end up with this scenario:
...
$ a
Performing
^C
$ a
Performing
^C
$ echo "${FUNCNAME[@]}"
a a a a a a a a a source
To demonstrate the issue, let's have another function, b, that gives an message_error function, also defined below:
$ function message_error() {
> local MESSAGE="$1"
>
> # FUNCNAME[1], as [0] is `message_error`, gets the calling function
> echo "[[ ERROR ]] ${FUNCNAME[1]}: $MESSAGE"
> echo "Rest of the stack: ${FUNCNAME[@]}"
>}
$ function b() { message_error "Oh no"; }
$ b
[[ ERROR ]] b: Oh no
Rest of the stack: message_error b a a a a a a a a a source
Even though the function is called in the source, a shows up as the calling function. As you can see, all these a functions were exited. However they remain in the FUNCNAME array.
Why is this? How can I remove it after SIGINT fails to remove it? Please note all functions are sourced from a large collection of commonly used functions, ~/.bash_functions, if sourcing the functions affects how they interpret SIGINT.
trap 'exit' SIGINT^C. I don't want to tarnish the ability to interrupt a script mid-run, I simply want the exec trace to be an accurate representation of what's happening.trap 'exit' SIGINTone line below the shebang of~/.bash_functions. Then I saved the script and opened a new terminal window, and ran functiona. The terminal window exited upon pressing^C. Sorry, forgot to mention that~/.bash_functionsis sourced by~/.bashrc, which then makes use of the functions within it's script upon terminal start.trap ' ' SIGINTis not the same astrap '' SIGINT. How nice.