The Linux/Unix Command Expect

Use a script to talk to other interactive programs

The Linux command expect talks to other interactive programs according to a script. Following the script, expect knows what can be "expected" from a program and what the correct response should be. An interpreted language provides branching and high-level control structures to direct the dialog. In addition, the user can take control and interact directly when desired, afterward returning control to the script.

The name expect comes from the idea of send/expect sequences popularized by uucp, kermit, and other modem-control programs. However, unlike uucp, expect is generalized so that it can be run as a user-level command with any program and task in mind.

People communicating encrypted data using cloud computing
 Roy Scott / Getty Images

What Expect Can Do

For example, here are some things the expect command can do:

  • Signal your computer to dial you back so that you can log in without paying for the call.
  • Start a game and if the optimal configuration doesn't appear, restart it again and again until it does, and then hand over control to you.
  • Run fsck, and in response to its questions, answer yes or no or give control back to you, based on predetermined criteria.
  • Connect to another network and automatically retrieve your mail so that it appears as if it were originally sent to your local system.
  • Carry environment variables, current directory, or any kind of information across rlogin, telnet, tip, su, or chgrp.

There are a variety of reasons why the shell cannot perform these tasks. All are possible with expect.


Expectk is a mixture of expect and Tk. It behaves just like expect and Tk's wish. Expect can also be used directly in C or C++ without Tcl.

The command accepts the following general form:

expect [options] cmdfile

Expect reads cmdfile for a list of commands to execute. Expect might be invoked implicitly on systems that support the #! notation by marking the script as executable and making the first line in the script:

#!/usr/local/bin/expect -f

Of course, the path must accurately describe where expect lives; /usr/local/bin is just an example.

The -c flag prefaces a command to be executed before any in the script. The command should be quoted to prevent it being broken up by the shell. Several commands may be executed with a single -c by separating them with semicolons. Commands are executed in the order they appear. When using expectk, this option is specified as -command.

The -d flag enables some diagnostic output, which primarily reports the internal activity of commands such as expect and interact.

The -D flag enables an interactive debugger. An integer value should follow. The debugger will take control before the next Tcl procedure if the value is non-zero or if a Ctrl+C is pressed or a breakpoint is hit, or another appropriate debugger command appears in the script.

The -f flag prefaces a file from which to read commands. The flag itself is optional as it is only useful when using the #! notation, so that other arguments may be supplied on the command line.

By default, the command file is read into memory and executed in its entirety. It is occasionally desirable to read files one line at a time. In order to force arbitrary files to be handled this way, use the -b flag.

The -i flag causes Expect to interactively prompt for commands instead of reading them from a file. Prompting is terminated via the exit command or upon EOF. The -i flag is assumed if neither a command file nor -c is used.

The command supports several additional options. Consult the manpage for expect for additional guidance and usage notes.

Was this page helpful?