[Shell command] Create a temporary file with mktemp

Created on November 30, 2026

mktemp creates a file with a unique name and prints its path. This example saves the returned path, writes a line, displays it, and removes the file.

tmpfile=$(mktemp)
printf 'hello\n' > "$tmpfile"
cat "$tmpfile"
rm "$tmpfile"
hello

Quote "$tmpfile" when passing the path to a command. Use mktemp -d to create a temporary directory.

Reference

Shell command