Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > gnu.bash.bug > #16147

Trap in command substitution breaks parent

From DALECKI Léo <leo.dalecki@ntymail.com>
Newsgroups gnu.bash.bug
Subject Trap in command substitution breaks parent
Date 2020-04-17 11:31 +0200
Message-ID <mailman.469.1587115911.3066.bug-bash@gnu.org> (permalink)
References <8994c3a1-024b-0193-cb72-a1a20120c9c7@ntymail.com>

Show all headers | View raw


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

Description:
     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

Repeat-By:

     First, copy the following script to script.sh

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

     trap catch INT

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

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

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


Back to gnu.bash.bug | Previous | Next | Find similar


Thread

Trap in command substitution breaks parent DALECKI Léo <leo.dalecki@ntymail.com> - 2020-04-17 11:31 +0200

csiph-web