bash: exit from source-d script
It seems there could be 2 parts to this. The first is to know when you're executing in a source
d script instead of an executed one. That can be found in this answer, and for bash they suggest:
[[ "$0" != "$BASH_SOURCE" ]] && sourced=1 || sourced=0
to tell whether or not you are being sourced.
If you are being sourced, you'd want to use return
instead of exit
.
You could store that function in a variable with something like
if [[ $sourced -eq 1 ]]; then
ret=return
else
ret=exit
fi
and then when you want to use the appropriate one you'd just use
$ret 1
Define:
[[ "$0" == "$BASH_SOURCE" ]] && ret=exit || ret=return
Use:
$ret 1