Hi,
I find the docco for return [n] not easy to interpret. Here is what can be read.
return [n]
Stop executing a shell function or sourced file and
return the value specified by n to its caller.
...
Any command associated with the RETURN trap is exe‐
cuted before execution resumes after the function or
script.
The later sentence is not true, and this lead to time consuming research with
some occurences in SO as well as bash-help list back in 2018.
As some are refining the documentation may be it is worth some more precision
Here is a test case.
$ echo $BASH_VERSION
5.2.21(1)-release
$ function f { :; }
$ trap "echo catch return" RETURN
$ . /dev/null # Hurray!
catch return
$ f # huh ?
$ shopt -s extdebug
$ f # ha!
catch return
As any gurus out there knows the function return trap is only available with
shopt -s extdebug, that is not explicitly stated in the documentation, and its
a pain.
I don't know the impact on perf of this extdebug thing and I am a bit reluctant
to have all my prod script turning extdebug on for the sake of trapping the
return.
So may be instead of cheating and fixing the docco with a mention of this
extdebug thing, a real fix could be better.
What do you think?
Cheers