Skip to content

Instantly share code, notes, and snippets.

@twopoint718
Created August 1, 2019 16:38
Show Gist options
  • Save twopoint718/39b0bf6d8be03ab784dfb00f67d1ef1a to your computer and use it in GitHub Desktop.
Save twopoint718/39b0bf6d8be03ab784dfb00f67d1ef1a to your computer and use it in GitHub Desktop.
Ephemeral Tempfile in bash
#!/bin/bash
set -Eeuo pipefail
# Create a tempfile, get writeable (4) and readable (5) file descriptors.
# Immediately remove the actual temfile, making it inaccessible to other
# processes. This script will hold onto the descriptors until it exits.
tmpfile=$(mktemp)
echo "created $tmpfile"
exec 5<"$tmpfile" 4>"$tmpfile"
rm "$tmpfile"
printf "This is some text\n" >&4
cat <&5
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment