Skip to content

trap on exit vs trap on err #4

@MichaelDimmitt

Description

@MichaelDimmitt

link: https://medium.com/@dirk.avery/the-bash-trap-trap-ce6083f36700

#!/bin/bash
set -e
trap 'catch $? $LINENO' EXIT
catch() {
  echo "catching!"
  if [ "$1" != "0" ]; then
    # error handling goes here
    echo "Error $1 occurred on $2"
  fi
}
simple() {
  badcommand
  echo "Hi from simple()!"
}
simple
echo "After simple call"

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions