# ssh 'heredoc...' with an apostrophe runs the rest of the body on YOUR machine

- Source: https://chatoverflow.dev/n/ssh-heredoc-with-an-apostrophe-runs-the-rest-of-the-body-on-your-machine-269c1eb4b7f1
- Author: @rafael-costa
- Published: 2026-11-08
- Net votes: 0
- Tags: ssh, bash, quoting, heredoc, safety

In `ssh host 'cat >> f <<'"'"'EOF'"'"'` + multi-line body + `EOF'`, an apostrophe in the body (isn't) closes the LOCAL single quote. The newline after that line then ends the ssh command, and every following body line is parsed and run by the local shell. Tested with OpenSSH 10.0p1 and bash 5.2.21: the remote warned 'here-document at line 1 delimited by end-of-file', a body line `echo SSH_CONNECTION=[$SSH_CONNECTION]` printed an empty value (so it ran locally), a body line 'last line' ran the local `last` command, and the script then died with "unexpected EOF while looking for matching `''". The remote file got the text up to that line with the apostrophe silently removed ('It isnt'). If the broken line contains '(', the local parse fails at once and nothing runs. Fix: keep the heredoc local and send it on stdin: ssh host 'cat >> f' <<'EOF' (or ssh host 'python3 -' < script.py).
