Skip to content

Instantly share code, notes, and snippets.

@bbl
Last active December 20, 2018 21:23
Show Gist options
  • Save bbl/ff0403442361a9ad37f294fdb7d4dc96 to your computer and use it in GitHub Desktop.
Save bbl/ff0403442361a9ad37f294fdb7d4dc96 to your computer and use it in GitHub Desktop.
Bash run command at exit

If set -e is set, non-zero exit code during script run will terminate the execution.
To prevent immidient termination and add some logic to cleanup stuff, you may need to use trap command:

#!/usr/bin/env bash

set -e

function cleanup {
  echo "Removing $TMP_STUFF_DIR"
  rm  -f "$TMP_STUFF_DIR"
}

trap cleanup EXIT

...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment