Skip to content
Latchkey

expect: Automate Interactive Prompts in CI

expect drives interactive programs by spawning them, waiting for expected output patterns, and sending scripted responses, so a password or yes/no prompt can be answered without a human.

Some tools insist on a real terminal and refuse stdin redirection. expect fakes the human: it watches for the prompt and types the answer. It is a last resort, but sometimes the only one.

What it does

expect (a Tcl-based tool) runs a child program under a pseudo-terminal with spawn, then alternates expect "pattern" (block until the program prints that text) and send "reply\r" (type a response). interact hands control back to the user; set timeout bounds how long to wait for a pattern.

Common usage

deploy.exp
#!/usr/bin/expect -f
set timeout 20
spawn ssh-keygen -t ed25519 -f /tmp/key
expect "Enter passphrase*"
send "\r"
expect "Enter same passphrase*"
send "\r"
expect eof

Options

CommandWhat it does
spawn <cmd>Launch a program under a pseudo-terminal
expect "<pat>"Block until the program output matches the pattern
send "<text>"Type text to the program (use \r for Enter)
set timeout <s>Seconds to wait for a pattern before timing out (-1 = forever)
expect eofWait for the program to finish
interactReturn control to the interactive user

In CI

Prefer the tool’s own non-interactive flags (a --password-stdin, an env var, a config file) before reaching for expect; scripted passwords in expect files leak secrets into logs and repos. When unavoidable, set a finite timeout so a changed prompt fails fast instead of hanging the job, and read secrets from the environment, not literals.

Common errors in CI

"expect: command not found" (install via apt-get install -y expect or brew install expect). "expect: spawn id ... not open" or a timeout means the program exited or printed a different prompt than your pattern; the prompt text changed across versions. A script that hangs has set timeout -1 (wait forever) plus a pattern that never appears; use a real timeout and expect eof.

Related guides

Run this faster and cheaper on Latchkey managed runners. Start free →