Bash – Error exit script from within command substitution

bashcommand-substitutionerror handling

If I have a function:

myfunction() {
  [ -d somedirectory ] && [ "some other condition" ] || { echo "error" >&2 ; exit 1; }
  global_var=somevalue
}

And I call it from within another function:

some_other_function() {
  myfunction
  # Do something with "$global_var"
}

This works as expected: If the conditions in myfunction fail, the error exit kills the whole script to prevent additional code from executing.

In refactoring a very large script that shares function definitions with other scripts (in a sourced file), I want to get rid of some of the global variables by returning them like so:

myfunction() {
  [ -d somedirectory ] && [ "some other condition" ] || { echo "error" >&2 ; exit 1; }
  local somevar=somevalue
  # do stuff with "$somevar", then...
  # return value of somevar to calling function.
  printf %s "$somevar"
}

some_other_function() {
  local anothervar="$(myfunction)"
  # Do something with "$another_var"
}

However, the error exit here fails to work as intended. Instead of killing the script, it only kills that function, which is executed in a subshell because of command substitution.

Is there a way to modularize this large script to allow text values to be returned from functions (rather than using global variables) and still allow functions to error exit from the entire script?

Best Answer

You have to send a signal to the main shell:

# start of the main script:
MAIN_SHELL_PID=$$

[...]

myfunction() {
    ... || { echo "error" >&2 ; kill -HUP "$MAIN_SHELL_PID"; }
}
Related Question