Overview
Script variables let you write reusable markdown scripts with customizable parameters. Users can override variables from the command line without editing the script itself.YAML Front-Matter Syntax
Declare variables in a YAML front-matter block at the top of your script:Syntax Rules
- Opt-in: Variables only activate when front-matter contains
vars:. Novars:= no behavior change. - Front-matter stripped: The
---block is removed from the prompt whenvars:is present. - Default values: Variables can have default values (like
topic: "machine learning") - No default: Variables declared with just the key (like
audience:) have no default value
Placeholder Substitution
Use{{varname}} syntax to insert variable values into your prompt:
{{placeholder}} syntax:
- Doesn’t collide with shell variables (
$VAR) - Doesn’t collide with markdown syntax
- Doesn’t collide with Claude syntax
- Is visually distinct and easy to read
Unset Variables
A variable declared with no default (justkey:) and no CLI override leaves {{key}} as-is in the prompt:
--topic flag: “Summarize .” (literal text)
With --topic "AI": “Summarize AI.”
CLI Overrides
Override variables from the command line using flag syntax:Examples
Given this script:summarize-topic.md
Mixing with AI Runner Flags
Variable overrides mix freely with AI Runner flags and provider overrides:How Flag Consumption Works
Override flags matching declared var names are consumed by the variable system — they don’t pass through to Claude Code:--topic,--style,--lengthare consumed (match declared vars)--live,--aws,--opuspass through to AI Runner- Unrecognized flags (like
--verbose) pass through to Claude Code
Complete Example
Here’s a real-world script with variables:summarize-topic.md
Usage Examples
Default behavior:Advanced Patterns
Optional vs Required Variables
Optional (with default):--target_file and --operation or the placeholders remain literal.
Multiple Scripts with Shared Variables
Create a family of scripts that accept the same variables:analyze.md
review.md
Pipeline with Variables
Pass variables through a pipeline:Common Use Cases
1. Documentation Generator
generate-docs.md
2. Test Runner
run-tests.md
3. Code Reviewer
review.md
4. Data Analyzer
analyze-data.md
Best Practices
Do:
- Provide sensible defaults for optional parameters
- Use descriptive variable names (
source_dirnotsrc) - Document expected values in comments
- Keep variable names lowercase with underscores
- Use boolean variables for feature flags (
verbose: false)
Troubleshooting
Variables Not Substituting
Problem:{{topic}} appears literally in output
Solutions:
- Check YAML syntax (must have
vars:key) - Ensure front-matter is at the top of the file
- Verify closing
---after front-matter - Check variable is declared in
vars:block
CLI Override Not Working
Problem:--topic "AI" doesn’t override the default
Solutions:
- Check variable is declared in front-matter
- Verify flag syntax:
--topic valueor--topic="value" - Check for typos in variable name
- Ensure flag comes after script name:
./script.md --topic "AI"
Conflicts with Claude Code Flags
Problem:--format json is interpreted as a variable instead of a Claude Code flag
Solution: Don’t declare variables with names that match Claude Code flags. Common conflicts to avoid:
--live--quiet--output-format--max-turns