zippy, my static-Python build tool, installed a bin/activate that was six lines long and valid in two languages at once. The whole file:
#!python -mzippy.activate
""":"
. /dev/stdin <<<"$(IFS=; RUN=$PWD/$BASH_SOURCE; ${RUN/#*\/\//\/})"
":"""To Python this is a shebang plus a module docstring—the body is inert string data. To bash the shebang is a comment, and the two docstring delimiters are spelled so each collapses to the : builtin: """:" is the empty string concatenated with ":", which is the word :, a no-op. Bash executes a comment, a no-op, the middle line, a no-op. Zero duplicated logic between the two readings.
The middle line does everything. Inside the command substitution, IFS= disables word splitting so paths with spaces survive, and RUN=$PWD/$BASH_SOURCE unconditionally prefixes the current directory onto however the script was sourced. Then ${RUN/#*\/\//\/}—a greedy prefix replace of *// with /—and the expansion is executed as a command: the script runs itself.
That last hop carries a historical portability defect. The kernel does not search PATH for a shebang interpreter; bare python is a relative pathname. Direct execution therefore reaches the environment’s embedded interpreter only when a suitable python resolves from the current working directory on a platform that accepts the relative shebang. Sourcing the file from an arbitrary directory can fail. Zippy’s install path deliberately passed --executable=python, so this was not merely an unreplaced source placeholder. When the layout/cwd invariant holds, -mzippy.activate prints export VIRTUAL_ENV=... and the PATH surgery to stdout. The substitution captures those lines and . /dev/stdin <<<"..." sources them into the current shell.
Activation also emits its own undo:
fn = 'deactivate () { __deactivate__=%s . %s; unset deactivate; }\n'deactivate re-sources the same six-line file with a marker variable set; the Python side sees __deactivate__ in the environment and prints the reversal—unset VIRTUAL_ENV, remove the bin dir from PATH—instead of the activation. One script, both directions, selected by an environment variable.
The gotcha is the // collapse. When you source the file by an absolute path, $PWD/$BASH_SOURCE produces a doubled slash at the seam, and the greedy replace strips $PWD back off by rewriting everything through that // to a single /. A relative source path never contains //, never matches, and rides through already correct. The expansion is therefore welded to the seam being the only doubled slash: a path that carries its own // anywhere would be over-stripped by the greedy match.