Consider foo.sh:
#!/bin/bash
function foo() {
source another.sh
echo "This shouldn't be executed. Return code: $?"
return 0
}
foo
echo "Return code: $?"
Then another.sh:
echo "Inside another.sh"
return 1
Running ./foo.sh prints:
Inside another.sh
This shouldn't be executed. Return code: 1
Return code: 0
Is there an alternative method to include a source file into another such that the return command would return from the function enclosing include command rather than from the command itself?
One option is to propagate the return code in foo.sh:
Then:
Alternatively, exit from the whole script in another.sh:
Then: