Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

preexec called twice when SIGINT trap is used #162

Open
danieltodor opened this issue Aug 10, 2024 · 1 comment
Open

preexec called twice when SIGINT trap is used #162

danieltodor opened this issue Aug 10, 2024 · 1 comment

Comments

@danieltodor
Copy link

Function added to preexec_functions is called twice when it is also added to a SIGINT trap and ^C is pressed.

Test code for bash:

test() {
    echo $1
}

trap "test sigint" INT

preexec_functions+=(test)

source ~/.bash-preexec.sh

PS1=""

Without the trap the output has no problem when i press ^C 3 times. The preexec was not called.

❯ ^C
❯ ^C
❯ ^C
❯

But when the trap is enabled then the test func is called 2 times per each ^C. Once with the last command from preexec, once with the argument from the trap.

clear
sigint

clear
sigint

clear
sigint

❯

With a similar test code, zsh doesn't have this weird behavior. test is only called from the trap.

❯ sigint

❯ sigint

❯ sigint

❯

The zsh equivalent test code:

test() {
    echo $1
}

TRAPINT() {
    test sigint
    return $(( 128 + $1 ))
}

autoload -Uz add-zsh-hook
add-zsh-hook preexec test

PROMPT=""
@akinomyoga
Copy link
Contributor

Due to the current approach of preexec using the DEBUG trap, I think there is no way to distinguish the execution of a trap handler from that of a user command. In Bash 5.3+, one might use PS0 + funsub as a more robust implementation of preexec.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants