This question is a variation on questions that have been asked before, e.g. How to return an error from a sourced bash script.
What I want, however, is for a sourced function X to call a second function Y, and then return function Y's return code back to the caller.
At the moment, I have got my script working using this pattern:
function_x() {
function_y || return $?
}
function_y() {
return 1
}
In practice, however, this leads to lots of return statements all through my code.
Suppose that function Y is in fact a usage function. I then end up with ugly code, that is not very DRY, like this:
_usage() {
echo "Usage: $0 [-h] OPTIONS"
return 0
}
function_a() {
[ "$1" == "-h" ] && _usage && return 1
some_other_condition && _usage && return 1
yet_another_condition && _usage && return 1
...
}
(My actual code is here.)
Is there a clean way, in general, for a function X to call a second function Y and then return Y's return code back to the caller?