Skip to content

Instantly share code, notes, and snippets.

@ezhuk
Last active January 1, 2016 11:29
Show Gist options
  • Save ezhuk/8138155 to your computer and use it in GitHub Desktop.
Save ezhuk/8138155 to your computer and use it in GitHub Desktop.
Create a temporary file the right way.
#!/bin/bash
#
# This creates a temporary file and sets up a trap handler so that it gets
# deleted when the script exits.
BASENAME=$(which basename)
MKTEMP=$(which mktemp)
RM=$(which rm)
TMP_PREFIX=$($BASENAME "$0")
TMP_FILE=$($MKTEMP -t ${TMP_PREFIX}.XXXXXX)
trap '$RM -f $TMP_FILE' EXIT
# Do something with the file.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment