Bash Error Guide: 'cannot create temp file for here-document' — Fix It
Fix Bash 'cannot create temp file for here-document': free space or inodes on /tmp, fix TMPDIR permissions, and stop noexec/full mounts breaking heredocs.
- #bash
- #automation
- #troubleshooting
- #errors
Stuck on this Bash & Python Automation error? Get the free incident triage checklist
A one-page PDF — the exact steps to isolate, fix, and verify a production error like this one. No spam, unsubscribe anytime.
Overview
Bash prints cannot create temp file for here-document when it tries to spool a here-document (<<EOF) to disk and the filesystem backing its temp directory refuses:
./report.sh: line 30: cannot create temp file for here-document: No space left on device
To feed a here-document to a command’s stdin, Bash writes the body to a temporary file (under $TMPDIR, default /tmp) and redirects from it. If that directory is full, out of inodes, read-only, or not writable by the user, the temp file can’t be created and the whole command fails. The trailing reason (No space left on device, Permission denied, Read-only file system) tells you which.
Symptoms
- Scripts using
<<EOFhere-documents suddenly fail while simpler commands still work. - The message always ends with an OS reason:
No space left on device,Permission denied, orRead-only file system. - It fires on a specific host (a full
/tmp, a hardened mount) but not others. df -h /tmpshows 100% used, ordf -i /tmpshows 100% inodes even with free bytes.- Cron jobs fail here while interactive runs succeed, or vice versa, because
TMPDIRdiffers.
Common Root Causes
/tmp(or$TMPDIR) is full — no free bytes; the temp file write fails withNo space left on device.- Inodes exhausted — plenty of free space but no free inodes (millions of tiny files), same symptom.
$TMPDIRpoints somewhere unwritable — a stale or wrongTMPDIRexport, or a directory owned by another user.- Read-only or full container layer — a container
/tmpmounted read-only or a tmpfs sized to near zero. - Quota exceeded — a per-user disk quota hit even though the volume has room.
- Permissions/sticky-bit damage on
/tmp— someone changed/tmpfrom1777so non-root users can’t create files.
Diagnostic Workflow
Find out which directory Bash uses and whether it’s writable:
echo "TMPDIR=${TMPDIR:-/tmp}"
touch "${TMPDIR:-/tmp}/.probe.$$" && echo "writable" && rm -f "${TMPDIR:-/tmp}/.probe.$$"
Check space and inodes — either being full causes the error:
df -h "${TMPDIR:-/tmp}" # bytes free
df -i "${TMPDIR:-/tmp}" # inodes free
Confirm mount flags (read-only? noexec?):
findmnt -T "${TMPDIR:-/tmp}"
Reproduce minimally:
cat <<EOF
probe
EOF
Find what’s filling /tmp:
du -xhd1 /tmp 2>/dev/null | sort -h | tail
find /tmp -xdev -type f -printf '%s %p\n' 2>/dev/null | sort -rn | head
Example Root Cause Analysis
A nightly reporting job failed only after midnight:
report.sh: line 30: cannot create temp file for here-document: No space left on device
The script piped a here-document into mail:
mail -s "Nightly report" ops@example.com <<EOF
$(generate_summary)
EOF
df -h /tmp showed 100% used, but df -h / showed 40% free — /tmp was a small dedicated tmpfs. du found the cause: a sibling job wrote multi-GB scratch files to /tmp and never cleaned them:
6.1G /tmp/exporter-scratch
Two fixes applied together. First, point this job at a roomier, self-cleaning temp dir:
export TMPDIR="$(mktemp -d /var/tmp/report.XXXXXX)"
trap 'rm -rf "$TMPDIR"' EXIT
Second, make the offending exporter clean up with its own trap ... EXIT and use mktemp instead of fixed names. With /tmp no longer saturated, the here-document spooled normally.
Prevention Best Practices
- Monitor
/tmpspace and inodes — alert well before 100%; bothdf -handdf -imatter. - Set a sane
TMPDIRfor jobs that produce large here-documents — e.g./var/tmp(persistent, usually larger) rather than a tiny tmpfs/tmp. - Always clean up temp dirs with
trap 'rm -rf "$TMPDIR"' EXITso scratch files don’t accumulate. - Use
mktemp -dfor per-run temp directories instead of fixed paths that collide or leak. - Verify container
/tmpis writable and sized — mount a tmpfs with enough capacity, not read-only. - Keep
/tmpat mode1777with the sticky bit so all users can create files safely.
Quick Command Reference
df -h "${TMPDIR:-/tmp}" # bytes free on the temp filesystem
df -i "${TMPDIR:-/tmp}" # inodes free (often the hidden cause)
findmnt -T "${TMPDIR:-/tmp}" # mount flags: ro? noexec? tmpfs size?
export TMPDIR="$(mktemp -d /var/tmp/job.XXXXXX)" # roomier per-run tmp
trap 'rm -rf "$TMPDIR"' EXIT # guarantee cleanup
du -xhd1 /tmp | sort -h | tail # find what filled /tmp
Related Guides
- Bash Error Guide: ‘Permission denied’ — when the reason is access rights, not free space.
- Bash Error Guide: ‘ambiguous redirect’ — other redirection failures that abort a command.
- Bash & Python Error Guide: ‘Argument list too long’ (E2BIG) — a related resource limit that stops commands from running.
Conclusion
cannot create temp file for here-document is Bash failing to spool a <<EOF body to disk, and the trailing OS reason names the cause: a full or inode-exhausted /tmp, an unwritable or read-only TMPDIR, or a quota. Check df -h and df -i on the temp filesystem, verify it’s writable, and point large jobs at a roomier TMPDIR with a cleanup trap. Keep /tmp monitored and self-cleaning and here-documents stop failing under load.
Fixed it? Get 500 Bash & Python Automation & DevOps AI prompts — free
500 battle-tested, copy-paste AI prompts engineered by a senior systems engineer — every one with fill-in placeholders and safety/back-out notes. Drop your email and it's yours.
- 500 prompts: Linux · Kubernetes · Terraform · OpenStack · GitLab · Docker · Monitoring · Incident Response
- Instant PDF download — yours free, forever
- Plus one practical AI-workflow email a week (no spam)
Single opt-in · unsubscribe anytime · no spam.
Did this fix your issue?
Get 500 Battle-Tested DevOps AI Prompts — Free
500 battle-tested, copy-paste AI prompts engineered by a senior systems engineer — every one with fill-in placeholders and safety/back-out notes. Drop your email and it's yours.
- 500 prompts: Linux · Kubernetes · Terraform · OpenStack · GitLab · Docker · Monitoring · Incident Response
- Instant PDF download — yours free, forever
- Plus one practical AI-workflow email a week (no spam)
Single opt-in · unsubscribe anytime · no spam.