Exit hooks | skybert.net

Exit hooks


When you want to add a command or a function to be called whenever your script ends (and you'd like it to be more error proof than calling the method at the end of your script file, you can do it like this:

1
2
3
4
5
6
7
#! /usr/bin/env bash

function my_exit_hook() {
  echo "Cleaning up after you"
}

trap my_exit_hook EXIT

Pretty cool, eh? You can read more on traps here.


Licensed under CC BY Creative Commons License ~ gmail torstein.k.johansen @ gmail ~ twitter @torsteinkrause ~