I have a Python script that needs to run under my bash profile so that it can load the path to uv. Claude helped me find this pattern for these kinds of scripts:
#!/bin/bash
# /// script
# requires-python = ">=3.13"
# dependencies = ["requests"]
# ///
""":"
# bash polyglot wrapper — sources bash profile so uv is on PATH,
# then re-execs itself with uv run --script
if [ -z "$_MY_SCRIPT_LOADED" ]; then
export _MY_SCRIPT_LOADED=1
for f in "$HOME/.bash_profile" "$HOME/.bashrc" "$HOME/.profile"; do
[ -f "$f" ] && . "$f"
done
exec uv run --script "$0" "$@"
fi
"""
import requests
# ...
The main trick is to use a construct that is a multiline string in Python, and a one line no-op in Bash, namely:
""":"
# bash code here
"""
# python code here
Bash interprets the first as an empty string "" followed by a literal string :, and in Bash, the colon command is a no-op.
Python interprets the triple quote """ as the start of a multiline string, then ignores everything after it until the next triple quote """, and then proceeds like normal.