Heredoc, stdin and scripted-edit pitfalls
bashsshheredocstdinsudophppythonediting
Writing files through shell heredocs and one-off scripts is fast, and it fails in quiet ways. Two cases that cost real time.
PHP (or anything with $) written over SSH
ssh host "cat > file.php <<EOF ... EOF" lets the shell expand $variable inside the heredoc, so PHP
code arrives with its variables blanked out. The same happens with any unquoted delimiter.
- Quote the delimiter (<<'EOF') to disable expansion, and remember that the outer double-quoted ssh
command string is itself expanded locally first.
- Safer: write the file locally (e.g. under /tmp), then scp it, or better, keep server code in a git
repo and ship it with the project's deploy script.
- Files copied in via /tmp on the SELinux server need restorecon (SELinux file contexts and booleans on web servers).
Python string surgery on source files
A python3 heredoc doing .replace() and slicing on a large main.js once duplicated a block through
a bad slice and corrupted the file. For anything beyond a trivial unique replacement:
- git checkout the file to get back to a known state, then use a targeted exact-match edit tool.
- Assert that the search string occurs exactly once before replacing.
- Diff the result before committing.
A heredoc behind a failing &&
In prep && cat > file <<'EOF' ... EOF, if prep fails the cat never runs, so the file is
silently not written (or keeps stale content) and later steps use it anyway. Run the prep step on
its own line and check it, then write the file. (Keyboard builder notes, 2026-10-07.)
A command has exactly one stdin
Every command gets one standard input, and the last redirection on the command wins. Several
traps follow from that, all hit by agent sessions in October 2026:
- Heredoc plus < /dev/null. bash <<'EOF' < /dev/null followed by a script body (or an
ssh host bash <<'EOF' with < /dev/null added "to be safe") gives bash an empty stdin: the heredoc is
discarded and the script silently never runs, exit status 0. Pick one source. For long scripts,
write the script to a file and run the file (umlol, 2026-10-30).
- A stray reader blocks forever. A leftover cat > /dev/null (or any command that reads stdin)
inside a tool call with no input waits until the call's timeout, 15 minutes in Claude Code. If a
command hangs with no output and no CPU, suspect something waiting on stdin.
- sudo -S reads the password from stdin. So echo "$PW" | sudo -S cmd < /dev/null fails:
the redirect replaces the pipe and sudo never sees the password (hub work, 2026-10-30).
- sudo eats the first piped line. echo "text" | sudo -S tee -a file gives text to sudo as
the password attempt, so nothing is appended. And piping a whole script into sudo -S bash
loses its first line the same way. Send the password on stdin and the data some other way:
echo "$PW" | sudo -S -p '' sh -c 'echo "text" >> file', or upload the script and run
echo "$PW" | sudo -S -p '' bash /tmp/script.sh (allaroundplumbingok.com, 2026-10-27).
- sudo -p '' suppresses the prompt text, which would otherwise end up mixed into captured output.
See also
- Deploy scripts with rsync (patterns and traps)
- SELinux file contexts and booleans on web servers
- Driving a desktop from a headless agent shell (DISPLAY, clipboard, sudo hand-off)
- Shell and process gotchas for agent sessions on the desktop