Skip to main content
The execute-template command executes one or more templates and writes the output to stdout. This is useful for testing templates and template functions.

Usage

chezmoi execute-template [template]...

Description

The execute-template command evaluates templates with access to all template data and functions available in chezmoi, including:
  • Configuration data (. variables)
  • Template functions (lookupSecret, output, etc.)
  • Chezmoi metadata (.chezmoi.* variables)
If no arguments are provided, reads a template from stdin. If arguments are provided, they are treated as template strings (or filenames with --file).

Flags

-f, --file
boolean
default:"false"
Treat arguments as filenames containing templates rather than template strings.
-i, --init
boolean
default:"false"
Simulate chezmoi init mode, which provides access to init-specific template functions like promptString, promptBool, etc.
--left-delimiter
string
default:"{{"
Set the left template delimiter.
--right-delimiter
string
default:"}}"
Set the right template delimiter.
-p, --promptString
key=value
Simulate promptString responses. Format: prompt=value. Can be specified multiple times.
--promptBool
key=value
Simulate promptBool responses. Format: prompt=true|false. Can be specified multiple times.
--promptInt
key=value
Simulate promptInt responses. Format: prompt=123. Can be specified multiple times.
--promptChoice
key=value
Simulate promptChoice responses. Format: prompt=choice. Can be specified multiple times.
--promptMultichoice
key=value
Simulate promptMultichoice responses. Format: prompt=choice1/choice2. Can be specified multiple times.
--stdinisatty
boolean
default:"false"
Simulate stdinIsATTY returning true.
--with-stdin
boolean
default:"false"
Set .chezmoi.stdin to the contents of stdin.

Examples

Execute a simple template string

chezmoi execute-template "{{ .chezmoi.os }}"
Output:
linux

Access configuration data

chezmoi execute-template "{{ .email }}"
Output:

Test template functions

chezmoi execute-template "{{ output \"uname\" \"-m\" }}"
Output:
x86_64

Execute a template file

chezmoi execute-template --file template.tmpl

Read template from stdin

echo "{{ .chezmoi.hostname }}" | chezmoi execute-template
Output:
mylaptop

Test init templates with prompts

chezmoi execute-template --init \
  --promptString "[email protected]" \
  --promptBool "useColor=true" \
  "Email: {{ .email }}, Color: {{ .useColor }}"
Output:
Email: [email protected], Color: true

Use custom delimiters

chezmoi execute-template \
  --left-delimiter "[[" \
  --right-delimiter "]]" \
  "OS: [[ .chezmoi.os ]]"

Execute multiple templates

chezmoi execute-template \
  "OS: {{ .chezmoi.os }}" \
  "Arch: {{ .chezmoi.arch }}"
Output:
OS: linux
Arch: amd64

Access stdin in templates

echo "Hello World" | chezmoi execute-template --with-stdin \
  "Input was: {{ .chezmoi.stdin }}"
Output:
Input was: Hello World

Template Testing

Test conditionals

chezmoi execute-template '{{ if eq .chezmoi.os "linux" }}Linux!{{ else }}Not Linux{{ end }}'

Test loops

chezmoi execute-template '{{ range list "a" "b" "c" }}{{ . }} {{ end }}'
Output:
a b c

Test secret managers

chezmoi execute-template '{{ (onepassword "item").password }}'

Debug template data

chezmoi execute-template '{{ . | toPrettyJson }}'
Prints all available template data in JSON format.

Init Template Testing

Test your .chezmoi.toml.tmpl configuration template:
chezmoi execute-template --init \
  --promptString "[email protected]" \
  --promptString "name=John Doe" \
  --promptBool "installDevTools=true" \
  --file .chezmoi.toml.tmpl

Terminal Output

$ chezmoi execute-template "My username is {{ .chezmoi.username }}"
My username is john

$ chezmoi execute-template '{{ .chezmoi | toPrettyJson }}'
{
  "arch": "amd64",
  "os": "linux",
  "hostname": "mylaptop",
  "username": "john"
}

Common Use Cases

Debug why a template isn’t working

# See the actual output
chezmoi execute-template --file ~/.local/share/chezmoi/dot_bashrc.tmpl

# Test specific expressions
chezmoi execute-template '{{ .nonexistent }}'

Test template functions before using them

# Test onepassword
chezmoi execute-template '{{ (onepassword "item") | toPrettyJson }}'

# Test bitwarden  
chezmoi execute-template '{{ (bitwarden "item") | toPrettyJson }}'

Validate init prompts

chezmoi execute-template --init \
  --promptString "[email protected]" \
  "{{ .email }}"
  • cat - View target contents of managed files
  • init - Initialize chezmoi with template prompts

Build docs developers (and LLMs) love