This function should exit the calling script:
crash() {
echo error
exit 1
}
This works as expected:
echo before
crash
echo after # execution never reaches here
But this does not:
echo before
x=$(crash) # nothing is printed, and execution continues
echo after # this is printed
How do I capture the result of a function, as well as allow it to exit?
This is because $(crash)
executes crash
in a subshell, so the exit
applies to the subshell and not to your script.
What is the point of capturing the output in a variable if you won't use it because the script exited anyway?
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments