❌ 'local' used on the same line with function call will prevent 'set -e' from aborting script

set -e

foo() {
  echo "This is foo; returns 1 (unhappy)"
  return 1
}

main() {
  echo "Calling foo()"
  # We succeed because of 'local' being used on the same line as the function call.
  local foo_capture="$(foo)"
  echo "After foo()"

  echo.green "MAIN FINISHED! (after foo calls), foo_capture=[${foo_capture}]"
}

main "${@}"

Output:

m:mac d:kotlin-mp b:master ○❯sr
Calling foo()
After foo()
MAIN FINISHED! (after foo calls), foo_capture=[This is foo; returns 1 (unhappy)]
m:mac d:kotlin-mp b:master ○❯

IF we were to split up declaration of foo_capture into

local foo_capture
foo_capture="$(foo)"

Then set -e would trigger and script would stop.


Backlinks