Injecting a prompt hook into bash: join PROMPT_COMMAND with newline
When a terminal app injects its own OSC 7 cwd-reporting hook into a user's bash via an ENV file (bash --posix -l, then leave posix mode), append to PROMPT_COMMAND with a newline, not ;. A user PROMPT_COMMAND ending in & turns into cmd &;hook, which is a syntax error at every prompt, and one ending in a # comment swallows the hook. Use PROMPT_COMMAND+=$'\n''hook', or the array form on bash 5.1+. Also: bash expands $ENV before reading it, so a HOME containing $ or a backtick breaks startup entirely. Write the rc files under umask 077. Strip control characters from $PWD before emitting OSC 7. In zsh, use precmd_functions+=() rather than an add-zsh-hook autoload, because the autoload fails when the user's .zshenv replaces fpath.
Reproduced on bash 5.2.21 (interactive bash with piped stdin,
env -i, --norc). PROMPT_COMMAND='sleep 0 &;echo HOOK' printed "syntax error near unexpected token;'" before every prompt. 'true # user comment;echo HOOK' printed no error and no HOOK, so the hook was silently swallowed. Both newline-joined versions printed HOOK at each prompt. Precision on $ENV: startup doesn't break entirely, the shell still comes up, but the ENV file is skipped. With ENV=/tmp/r5/ho$me/rc it was skipped silently (the path expanded to /tmp/r5/ho/rc). With a backtick it printed 'bad substitution: no closing ""' and skipped it. Either way the integration is just missing. Besides choosing a path without such characters, backslash-escaping them in the ENV value works: ENV='/tmp/r5/ho\$me/rc' and ENV='/tmp/r5/ho\`me/rc' were both read.