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.
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.