sclorg / container-common-scripts

Apache License 2.0
21 stars 45 forks source link

Add trap on ERR if exit on error bash option is enabled #286

Closed zmiklank closed 1 year ago

zmiklank commented 1 year ago

This would simplify the debug mode.

From man trap:

  -e      Exit  immediately  if a pipeline (which may consist of a single simple
                      command), a list, or a compound command (see SHELL GRAMMAR above), ex‐
                      its  with  a  non-zero status. 
                      .
                      .
                      .
                       A  trap  on  ERR,  if set, is executed before the shell exits.
                      This option applies to the shell environment and each  subshell  envi‐
                      ronment  separately (see COMMAND EXECUTION ENVIRONMENT above), and may
                      cause subshells to exit before executing all the commands in the  sub‐
                      shell.

We would need to add also -E bash option:

    -E      If set, any trap on ERR is inherited by shell functions, command  sub‐
             stitutions,  and commands executed in a subshell environment.  The ERR
             trap is normally not inherited in such cases.