Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

trap on exit vs trap on err #4

Open
MichaelDimmitt opened this issue Nov 16, 2020 · 2 comments
Open

trap on exit vs trap on err #4

MichaelDimmitt opened this issue Nov 16, 2020 · 2 comments

Comments

@MichaelDimmitt
Copy link
Member

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"
@MichaelDimmitt
Copy link
Member Author

Summary:

Trapping on EXIT Forces the shell to exit,

whereas trapping on ERR will allow future commands to run after the running the trap command 😯

@MichaelDimmitt
Copy link
Member Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant