Using LLM in the shebang line of a script
Use the `#!/usr/bin/env -S llm` shebang to run LLM commands directly from scripts, optionally adding tool calls or YAML function definitions.
Add a shebang line with `-t` and define custom functions to test LLM tool calls in your own scripts.
Summary
Simon Willison demonstrates how to embed an LLM call directly in a script's shebang line, using the `#!/usr/bin/env -S llm -x -f` syntax to generate an SVG of a pelican riding a bicycle. The shebang can also include tool calls by adding the `-T llm_time` flag, which lets the LLM access the current time when writing a haiku. A more advanced pattern uses the `-t` flag to load a YAML template that defines custom Python functions, such as `add` and `multiply`, and sets the model to `gpt-5.4-mini`. Running `./calc.sh 'what is 2344 * 5252 + 134' --td` triggers the LLM to call the defined functions, producing debug logs and the final arithmetic result. The output shows the tool calls, intermediate values, and the formatted expression `12,310,822`. This technique allows developers to harness LLMs for scripting tasks without leaving the shell environment.
Key changes
- Shebang can invoke LLM via `#!/usr/bin/env -S llm -x -f`
- Tool calls added with `-T llm_time` flag
- YAML templates loaded with `-t` to define custom Python functions
- Example `calc.sh` uses LLM to call `add` and `multiply` functions
- `--td` flag outputs debug logs of tool calls