help-bash
[Top][All Lists]
Advanced

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

[Help-bash] & affect the order of EXIT trap executed in subshells


From: Peng Yu
Subject: [Help-bash] & affect the order of EXIT trap executed in subshells
Date: Tue, 18 Dec 2018 18:07:50 -0600

Hi,

But just adding '&' and `wait`, the order of execution of EXIT traps
are changed.

I am not sure that this is a good design as it is more intuitive if
the behavior is preserved as much as possible with such a minor change
(as the main purpose of & is to run processes in background it should
not touch how signals are handle as much as possible).

Given that normal background processes won't get the INT signal, it
seems to be special treatment for subshell for which to get the signal
later.

Is this a design decision that has undergone careful consideration?
Would it make more sense to make the behavior consistent between the
two scenarios? Is there a walkaround that can make the two scenarios
behave the same (still running the subshell in the background it, it
can run parallel if multiple subshells are started)? Thanks.

$ ./main.sh
(
    trap 'echo EXIT1' EXIT
    (
        trap 'echo EXIT2' EXIT
        sleep 3
    )
)
^Cecho EXIT2
EXIT2
echo EXIT1
EXIT1

$ cat \&/main.sh
#!/usr/bin/env bash
# vim: set noexpandtab tabstop=2:

set -v
(
    trap 'echo EXIT1' EXIT
    (
        trap 'echo EXIT2' EXIT
        sleep 3
    )  &
    wait
)
$ ./\&/main.sh
(
    trap 'echo EXIT1' EXIT
    (
        trap 'echo EXIT2' EXIT
        sleep 3
    )  &
    wait
)
^Cecho EXIT1
EXIT1
echo EXIT2
EXIT2

-- 
Regards,
Peng



reply via email to

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