On 08/03/2020 12:35, Dirk Fieldhouse wrote:
POSIX
<https://pubs.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html#return>
says, and has since at least 2004:
"The return utility shall cause the shell to stop executing the current
function or dot script. If the shell is not currently executing a
function or dot script, the results are unspecified."
[...]
As POSIX refers to subshells explicitly elsewhere (eg 'exit') it's
difficult to believe that "subshell" was accidentally omitted from the
list of contexts that 'return' should return from, but implementation
behaviours consistently contradict the spec as written. Can they be made
conformant without breaking existing scripts?
In the subshell, the shell should not be considered to still be
executing a function or dot script. As such, the results should be
unspecified, and any behaviour should be valid. The standard may be
underspecified here, but any other interpretation is not reasonable.
Subshells work by starting a new process. The parent process waits for
the subshell to finish and acts on its exit status. The child process
has very little ways to influence its parent process other than that,
and the parent process might not even still be running by the time the
child process gets to the return statement.
Cheers,
Harald van Dijk