[Top][All Lists]

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

Trap in command substitution breaks parent

Subject: Trap in command substitution breaks parent
Date: Fri, 17 Apr 2020 11:31:42 +0200
User-agent: Mozilla/5.0 (X11; Linux x86_64; rv:68.0) Gecko/20100101 Thunderbird/68.7.0

Machine: x86_64
OS: linux-gnu
Compiler: gcc
Compilation CFLAGS:  -DPROGRAM='bash' -DCONF_HOSTTYPE='x86_64' -DCONF_OSTYPE='linux-gnu' -DCONF_MACHTYPE='x86_64-pc-linux-gnu' -DCONF_VENDOR='pc' -DLOCALEDIR='/usr/share/locale' -DPACKAGE='bash' -DSHELL -DHAVE_CONFIG_H   -I.  -I../. -I.././include -I.././lib  -Wdate-time -D_FORTIFY_SOURCE=2 -g -O2 -fdebug-prefix-map=/build/bash-N2nMjo/bash-4.4.18=. -fstack-protector-strong -Wformat -Werror=format-security -Wall -Wno-parentheses -Wno-format-security uname output: Linux Shodan 4.15.0-96-generic #97-Ubuntu SMP Wed Apr 1 03:25:46 UTC 2020 x86_64 x86_64 x86_64 GNU/Linux
Machine Type: x86_64-pc-linux-gnu

Bash Version: 4.4
Patch Level: 20
Release Status: release

    A trap set with an external call in a command substitution seems to break the parent Bash shell.

    I have a script that displays a text-based user interface, it displays a menu to stderr and catches keyboards inputs with the read internal command.     To ensure that the display is reset when the script is interrupted, I need to trap the INT signal. The trapping function needs to call an external command.

    To retrieve the user's choice from the text-based interface, I use a command substitution: var="$(./script.sh)"     When the script is called within a command substitution, if the INT signal is sent by hitting CTRL and C, the parent shell seems to break: anything typed (including control characters) will be printed out until return is hit, then no inputs will be shown.     Removing the external command call in the trapping function seems to fix the issue (still, the echo doesn't seem to work), but I don't grasp why.

    I asked about it on Stack Overflow (https://stackoverflow.com/questions/61234233/trap-with-external-call-in-command-substitution-breaks-the-parent-bash-shell), other users were able to reproduce this behavior on newer versions (including 5.0.16(2)-maint).

    Manually running the following command afterwards seems to fix the issue: stty icanon echo echok


    First, copy the following script to script.sh

    catch() {
        echo "caught"
        ps # Calling an external command

    trap catch INT

    while read -sN1; do # Reading from the keyboard
        echo $REPLY >&2

    Second, execute it within a command substitution: var="$(./script.sh)"

    And last, send the INT signal by hitting CTRL and C.

reply via email to

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