Path: csiph.com!xmission!news.snarked.org!news.linkpendium.com!news.linkpendium.com!panix!usenet.stanford.edu!not-for-mail From: =?UTF-8?Q?DALECKI_L=c3=a9o?= Newsgroups: gnu.bash.bug Subject: Trap in command substitution breaks parent Date: Fri, 17 Apr 2020 11:31:42 +0200 Lines: 81 Approved: bug-bash@gnu.org Message-ID: References: <8994c3a1-024b-0193-cb72-a1a20120c9c7@ntymail.com> NNTP-Posting-Host: lists.gnu.org Mime-Version: 1.0 Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: quoted-printable X-Trace: usenet.stanford.edu 1587115911 24220 209.51.188.17 (17 Apr 2020 09:31:51 GMT) X-Complaints-To: action@cs.stanford.edu To: bug-bash@gnu.org, bash@packages.debian.org Envelope-to: bug-bash@gnu.org X-EA-Auth: JHToq7T3ykRRtlDXoN9oW8ExI7UM3qioNxb60D5qH/ftjA7LCGhoPvZAMVa1o1njqAy/6/iaetlHHL+E1vDJ3TJ+rGtnEG0qgO5z4tspdyw= User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:68.0) Gecko/20100101 Thunderbird/68.7.0 Content-Language: en-US X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] [fuzzy] X-Received-From: 185.175.5.90 X-BeenThere: bug-bash@gnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: Bug reports for the GNU Bourne Again SHell List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Mailman-Original-Message-ID: <8994c3a1-024b-0193-cb72-a1a20120c9c7@ntymail.com> Xref: csiph.com gnu.bash.bug:16147 Machine: x86_64 OS: linux-gnu Compiler: gcc Compilation CFLAGS:=C2=A0 -DPROGRAM=3D'bash' -DCONF_HOSTTYPE=3D'x86_64'=20 -DCONF_OSTYPE=3D'linux-gnu' -DCONF_MACHTYPE=3D'x86_64-pc-linux-gnu'=20 -DCONF_VENDOR=3D'pc' -DLOCALEDIR=3D'/usr/share/locale' -DPACKAGE=3D'bash'= =20 -DSHELL -DHAVE_CONFIG_H=C2=A0=C2=A0 -I.=C2=A0 -I../. -I.././include -I../= ./lib=C2=A0=20 -Wdate-time -D_FORTIFY_SOURCE=3D2 -g -O2=20 -fdebug-prefix-map=3D/build/bash-N2nMjo/bash-4.4.18=3D.=20 -fstack-protector-strong -Wformat -Werror=3Dformat-security -Wall=20 -Wno-parentheses -Wno-format-security uname output: Linux Shodan 4.15.0-96-generic #97-Ubuntu SMP Wed Apr 1=20 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: =C2=A0=C2=A0=C2=A0 A trap set with an external call in a command substit= ution seems to=20 break the parent Bash shell. =C2=A0=C2=A0=C2=A0 I have a script that displays a text-based user inter= face, it=20 displays a menu to stderr and catches keyboards inputs with the read=20 internal command. =C2=A0=C2=A0=C2=A0 To ensure that the display is reset when the script i= s interrupted,=20 I need to trap the INT signal. The trapping function needs to call an=20 external command. =C2=A0=C2=A0=C2=A0 To retrieve the user's choice from the text-based int= erface, I use=20 a command substitution: var=3D"$(./script.sh)" =C2=A0=C2=A0=C2=A0 When the script is called within a command substituti= on, if the INT=20 signal is sent by hitting CTRL and C, the parent shell seems to break:=20 anything typed (including control characters) will be printed out until=20 return is hit, then no inputs will be shown. =C2=A0=C2=A0=C2=A0 Removing the external command call in the trapping fu= nction seems=20 to fix the issue (still, the echo doesn't seem to work), but I don't=20 grasp why. =C2=A0=C2=A0=C2=A0 I asked about it on Stack Overflow=20 (https://stackoverflow.com/questions/61234233/trap-with-external-call-in-= command-substitution-breaks-the-parent-bash-shell),=20 other users were able to reproduce this behavior on newer versions=20 (including 5.0.16(2)-maint). =C2=A0=C2=A0=C2=A0 Manually running the following command afterwards see= ms to fix the=20 issue: stty icanon echo echok Repeat-By: =C2=A0=C2=A0=C2=A0 First, copy the following script to script.sh =C2=A0=C2=A0=C2=A0 catch() { =C2=A0=C2=A0=C2=A0 =C2=A0=C2=A0=C2=A0 echo "caught" =C2=A0=C2=A0=C2=A0 =C2=A0=C2=A0=C2=A0 ps # Calling an external command =C2=A0=C2=A0=C2=A0 =C2=A0=C2=A0=C2=A0 exit =C2=A0=C2=A0=C2=A0 } =C2=A0=C2=A0=C2=A0 trap catch INT =C2=A0=C2=A0=C2=A0 while read -sN1; do # Reading from the keyboard =C2=A0=C2=A0=C2=A0 =C2=A0=C2=A0=C2=A0 echo $REPLY >&2 =C2=A0=C2=A0=C2=A0 done =C2=A0=C2=A0=C2=A0 Second, execute it within a command substitution: var= =3D"$(./script.sh)" =C2=A0=C2=A0=C2=A0 And last, send the INT signal by hitting CTRL and C.