help-bash
[Top][All Lists]
Advanced

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

Should trap be removed in pipeline?


From: Peng Yu
Subject: Should trap be removed in pipeline?
Date: Tue, 19 Jan 2021 14:44:46 -0600

Hi,

See below. Depending on how a pipeline is called, the trap inside the
pipeline may or may not be called.

$ bash --version | head -n 1
GNU bash, version 5.0.18(1)-release (x86_64-apple-darwin19.5.0)
$ cat ./main.sh
#!/usr/bin/env bash
true | trap 'echo called trap' EXIT
$  ./main.sh
$ cat ./main2.sh
#!/usr/bin/env bash
true | { trap 'echo called trap' EXIT; }
$  ./main2.sh
called trap

I don't think this behavior make much sense. The following is an
example. If trap is hidden inside a function, then the user must use
the form of { f; } in the pipeline to trigger the trap in the
function. When the function is very complex, it is impractical for
users to always remember whether a function calls trap or not.

Should be consider as a possible bug in bash, (therefore, get fixed)?

$ cat function/main.sh
#!/usr/bin/env bash
function f {
 trap 'echo called trap' EXIT
}
true | f
$ function/main.sh
$ cat function/main2.sh
#!/usr/bin/env bash
function f {
    trap 'echo called trap' EXIT
}
true | { f; }
$  function/main2.sh
called trap

I don't recall this behavior in early version bash, but I may not have
test this behavior specifically before.

Was this behavior introduced in some early version of bash? Or bash
has always been like this?

-- 
Regards,
Peng



reply via email to

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