Expect is a tool for automating interactive programs, written by Don Libes at the National Institute of Standards and Technology and presented in his paper “expect: Scripts for Controlling Interactive Processes.” The official manual page opens with the defining description: “Expect is a program that ‘talks’ to other interactive programs according to a script.” Where ordinary shell scripts can only pipe text between batch programs, Expect can drive programs that insist on talking to a human at a terminal.
The tool exists because many Unix programs deliberately bypass redirection and demand input straight from a terminal. Commands like passwd, telnet, ftp, and ssh prompt for passwords and answers interactively and refuse to read them from a pipe. Before Expect, automating such a program meant either patching it or giving up. Expect solved the problem from the outside by pretending to be the terminal a program expects to find.
Expect is built on Tcl, and the manual tells new users to learn four commands first, “spawn, send, expect, and interact, in that order.” The spawn command launches an interactive program and connects it to a pseudo-terminal. The expect command waits for a pattern of output, such as a “Password:” prompt. The send command types a response back as if a human were at the keyboard. The interact command hands control back to the live user when the script is done.
These few primitives turn brittle manual procedures into repeatable scripts. An administrator can write an Expect script that logs in to dozens of machines over telnet, runs a command, and collects the output, or one that changes passwords across many accounts by driving passwd. Libes’s paper catalogs a range of such problems that Expect solved, giving readers patterns they could apply directly.
Because Expect is itself a Tcl extension, it inherits Tcl’s full scripting language: variables, loops, conditionals, and pattern matching are all available around the spawn-and-expect core. Expect became a standard part of the Unix system administrator’s toolkit and remains the canonical answer to the problem of scripting a program that was never designed to be scripted.