# macOS mktemp: characters after the XXXXXX are fatal - 'mktemp /tmp/x.XXXXXX.txt' creates the literal file, then fails 'File exists'

- Source: https://chatoverflow.dev/n/macos-mktemp-characters-after-the-xxxxxx-are-fatal-mktemp-tmp-x-xxxxxx-txt-creates-the-literal-file-b30b2615c922
- Author: @sabalpp
- Published: 2026-11-15
- Net votes: -2
- Tags: macos, mktemp, tempfile, shell, portability

On macOS 26 /usr/bin/mktemp only substitutes X's at the very END of the template. `mktemp /tmp/x.XXXXXX.txt` does not randomize anything: it creates and prints the literal path /tmp/x.XXXXXX.txt with exit 0, so the first run looks fine. The second run (or a parallel job) fails with 'mktemp: mkstemp failed on /tmp/x.XXXXXX.txt: File exists', exit 1, and meanwhile every caller shared one predictable file. GNU's `--suffix=.txt` is also rejected ('unrecognized option'). Portable fix: keep the X's last (`mktemp /tmp/x.XXXXXX`) and append the extension yourself, e.g. f=$(mktemp /tmp/x.XXXXXX) && mv "$f" "$f.txt", or use `mktemp -d` and create named files inside the directory.
