[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
parent trap EXIT appears to subshell but is not used
From: |
Oğuz |
Subject: |
parent trap EXIT appears to subshell but is not used |
Date: |
Mon, 29 Mar 2021 21:28:11 +0300 |
29 Mart 2021 Pazartesi tarihinde Valentin Lab <valentin.lab@kalysto.org>
yazdı:
> Hi,
>
> I have encountered an issue when running some bash code from 4.3 that
> behave in an unexpected way on 4.4 or 5 ... I've managed to pinpoint the
> exact issue on my end.
>
> Here is the code:
>
>
> ## ----------------
> trap -- 'echo bar' EXIT
> (
> echo -n 'Subshell TRAP EXIT: '
> trap -p EXIT
> echo
> )
> echo main shell
>
> ##----------------
>
>
> Runnning this will give the same output on bash 4.4 and 5:
>
> ## ---- BEGIN OUTPUT
> Subshell TRAP EXIT: trap -- 'echo bar' EXIT
>
> main shell
> bar
> ## ---- END OUTPUT
>
>
> But will give this output on bash 4.3:
>
> ## ---- BEGIN OUTPUT
> Subshell TRAP EXIT:
> main shell
> bar
> ## ---- END OUTPUT
>
>
> We notice 2 important point:
> - Bash >4.3 trap's is displaying parent shell's EXIT trap, while
> 4.3 is not displaying it.
> - None of them will execute any trap at the end of the subshell.
>
>
> In 4.3, this makes sense to me. EXIT trap is not available and not
> executed.
>
> In >4.3, I don't understand: 'trap -p' is displaying a trap that is not
> really enabled ?
>
>
> Notice that we can set an EXIT trap in the subshell, and then all bash
> version will display AND execute the trap at the end of the subshell. Which
> seems totally okay to me.
>
>
> Is this strange behavior about bash >4.3 being able to display EXIT trap,
> but won't use them is expected ? I'd be happy to know about the rationale
> behind this if this is expected.
I think it is. Otherwise `exit_trap=$(trap -p exit)' wouldn't work.
>
>
> Many thanks,
>
> Valentin Lab
>
>
> PS: note that I found some other 'report' of that behavior here:
> https://unix.stackexchange.com/questions/282722#answer-374269
>
>
--
Oğuz