Skip to main content
edited tags
Link
lonix
  • 2k
  • 2
  • 23
  • 41
edited tags
Link
Vlastimil Burián
  • 31.1k
  • 66
  • 210
  • 358
Source Link
lonix
  • 2k
  • 2
  • 23
  • 41

Capture bash function's result and allow it to exit

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?