Pelo que entendi, há uma exceção ao snippet de documento na minha pergunta. O trecho foi:
If a sigspec is RETURN, the command arg is executed each time a shell function or a script executed with the . or source builtins finishes executing.
A exceção é descrita aqui:
All other aspects of the shell execution environment are identical between a function and its caller with these exceptions: the DEBUG and RETURN traps (see the description of the trap builtin under SHELL BUILTIN COMMANDS below) are not inherited unless the function has been given the trace attribute (see the description of the declare builtin below) or the -o functrace shell option has been enabled with the set builtin (in which case all functions inherit the DEBUG and RETURN traps), and the ERR trap is not inherited unless the -o errtrace shell option has been enabled.
Quanto a functrace
, pode ser ativado com typeset
' -t
:
-t Give each name the trace attribute. Traced functions inherit the DEBUG and RETURN traps from the calling shell. The trace attribute has no special meaning for variables.
Também set -o functrace
faz o truque.
Aqui está uma ilustração.
$ trap 'echo ko' RETURN
$ f () { echo ok; }
$ cat y
f
$ . y
ok
ko
$ set -o functrace
$ . y
ok
ko
ko
Quanto a declare
, é novamente a opção -t
:
-t Give each name the trace attribute. Traced functions inherit the DEBUG and RETURN traps from the calling shell. The trace attribute has no special meaning for variables.
Além disso, extdebug
ativa o rastreamento de funções, como na resposta do ikkachu .