Quick Linux Tip #20:
Try: TMPFILE=$(mktemp) && trap 'rm -f "$TMPFILE"' EXIT INT TERM
Info: mktemp creates a secure, unique file; trap catches signals like exit or Ctrl+C to guarantee cleanup.
Examples:
- $ TMPDIR=$(mktemp -d) # Secure temp directory
- $ trap 'rm -rf "$TMPDIR"' EXIT
- $ mktemp -t script-XXXXXX.log # Custom prefix
Note: Prevents dangling files during unexpected script crashes. Permissions default to 600 (owner-only), preventing local information leaks.
Leave a Reply