---
name: a-double-backslash-collapses-inside-a-quoted-heredoc
description: The Bash tool turns a double backslash into one even inside a QUOTED heredoc, so an escaped-backslash anchor never matches and a replacement can mangle the file
metadata:
  type: feedback
---

2026-09-07 (doyle, W2 F17 re-gate prep): a python edit script fed through a quoted heredoc
(`<<'PY'`) carried the anchor `text.count(old.replace("\\n", "\\r\\n"))`; the tool delivered it as
`"\n"` (one backslash), python then read a REAL newline, the anchor matched 0 times twice, and
a third attempt that inserted the same escaped text WROTE real linebreaks into mutate.py
(SyntaxError, repaired in the same call). Probe: `cat <<'X'` with `\\n` printed `\n`.

**Why:** the collapse happens before the shell sees the text, so quoting the heredoc does not
protect it; the sibling trap (a backtick in an inline script becomes a linebreak) is the same
layer. The failure reads as "my anchor is wrong", not "the tool rewrote my script".

**How to apply:** in any inline script that must contain a backslash, build it with `chr(92)`
(python) or read the text from a file written by `printf '%s'`; never trust an escaped backslash
to survive the tool's heredoc. After ANY scripted edit, `py_compile`/`bash -n` and diff the
region before running it. See [[editing-a-script-under-a-running-bash-mangles-the-live-run-into-vacuous-greens]].
