Skip to content

preexec called twice when SIGINT trap is used #162

@danieltodor

Description

@danieltodor

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=""

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions