undefined behaviour of return statement in /bin/sh and missing man entry

Mihir Luthra luthramihir708 at gmail.com
Mon Aug 19 17:10:07 UTC 2019


Hi everyone,

Recently I was writing script on my virtual machine where I needed to know
If a /bin/sh script has been sourced. I found many ways on stackoverflow,
most of which only seem to work for bash. Although after some research, I
found a way that should have worked with /bin/sh. It was that if I executed
return from a function or sourced script, it should work fine and in other
cases it should have wrote to stderr.

Although on executing return on command line I got logged out and on
executing same on non-login shells it just exited.
Also, there seem to be no entry for return statement on man page of sh. [1]

I have posted a brief explanation for the same in my stackoverfow question.
[2].

[1]
https://www.freebsd.org/cgi/man.cgi?query=sh&manpath=Unix+Seventh+Edition
[2]
https://stackoverflow.com/questions/57547626/detect-if-a-script-has-been-sourced-in-bin-sh

Kind Regards,
Mihir


More information about the freebsd-hackers mailing list