EXPECT(1)

๐Ÿ“ฆ version 5.45.4, in ubuntu stonking

๐Ÿ’– expect โ€” programmed dialogue with interactive programs, Version 5 ๐Ÿ’–

okay bestie, welcome to EXPECT(1), the manual page for expect ๐ŸŒˆ โ€” and no, this isn't the boring kind of manual page, this is the one that lets your computer hold a whole conversation with other programs while you sit there looking iconic. let's get into it โœจ


๐ŸŒธ NAME ๐ŸŒธ

expect โ€” programmed dialogue with interactive programs, Version 5 ๐Ÿ’…


โœจ SYNOPSIS โœจ

expect [ -dDinN ] [ -c cmds ] [ [ -[f|b] ] cmdfile ] [ args ]

like. that's it. that's the whole summoning circle ๐Ÿ•ฏ๏ธ


๐Ÿฆ‹ INTRODUCTION ๐Ÿฆ‹

so here's the tea, bestie: Expect is a program that literally talks to other interactive programs, following a script you wrote. it knows what it can expect from a program and what the correct response should be โ€” hence the name, iykyk ๐Ÿ˜Œ. it's a whole interpreted language, so you get branching and high-level control structures to direct the dialogue. and the best part? you can take the wheel and interact directly whenever you feel like it, and then hand control back to the script. main character energy, honestly ๐Ÿ‘‘

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++ (that is, without Tcl). see libexpect(3) for that โœจ

the name "Expect" comes from the idea of send/expect sequences that uucp, kermit and other modem control programs made popular. however, unlike uucp, Expect is generalized, so it can be run as a user-level command with any program and task in mind. and it can actually talk to several programs at the same time โ€” multitasking queen fr fr ๐Ÿ’…

for example, here are some things Expect can do:

  • Cause your computer to dial you back, so that you can login without paying for the call. ๐Ÿ’ธ
  • Start a game (e.g., rogue) and if the optimal configuration doesn't appear, restart it (again and again) until it does, then hand over control to you. ๐ŸŽฎ
  • Run fsck, and in response to its questions, answer "yes", "no" or give control back to you, based on predetermined criteria. ๐Ÿฉบ
  • Connect to another network or BBS (e.g., MCI Mail, CompuServe) and automatically retrieve your mail so that it appears as if it was originally sent to your local system. ๐Ÿ“ฌ
  • Carry environment variables, current directory, or any kind of information across rlogin, telnet, tip, su, chgrp, etc. ๐ŸŽ’

there are a variety of reasons why the shell cannot perform these tasks. (try, you'll see.) all are possible with Expect. no cap ๐Ÿซถ

in general, Expect is useful for running any program which requires interaction between the program and the user. all that's necessary is that the interaction can be characterized programmatically. Expect can also give the user back control (without halting the program being controlled) if desired. similarly, the user can return control to the script at any time. it's giving collaborative ๐Ÿค


๐Ÿ’… USAGE ๐Ÿ’…

Expect reads cmdfile for a list of commands to execute. Expect may also be invoked implicitly on systems which support the #! notation by marking the script executable, and making the first line in your script:

#!/usr/bin/expect -f

of course, the path must accurately describe where Expect lives. /usr/bin is just an example. don't come for me ๐Ÿ™ˆ

the -c flag prefaces a command to be executed before any in the script. the command should be quoted to prevent being broken up by the shell. this option may be used multiple times. multiple 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 internal activity of commands such as expect and interact. this flag has the same effect as "exp_internal 1" at the beginning of an Expect script, plus the version of Expect is printed. (The strace command is useful for tracing statements, and the trace command is useful for tracing variable assignments.) (When using Expectk, this option is specified as -diag.) ๐Ÿ”

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 ^C is pressed (or a breakpoint is hit, or other appropriate debugger command appears in the script). see the README file or SEE ALSO (below) for more information on the debugger. (When using Expectk, this option is specified as -Debug.) ๐Ÿ›

the -f flag prefaces a file from which to read commands from. the flag itself is optional as it is only useful when using the #! notation (see above), so that other arguments may be supplied on the command line. (When using Expectk, this option is specified as -file.) ๐Ÿ“‚

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. for example, stdin is read this way. in order to force arbitrary files to be handled this way, use the -b flag. (When using Expectk, this option is specified as -buffer.) note that stdio-buffering may still take place however this shouldn't cause problems when reading from a fifo or stdin. ๐Ÿงบ

if the string - is supplied as a filename, standard input is read instead. (Use ./- to read from a file actually named -.) ๐ŸŽฏ

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. see interpreter (below) for more information. -i is assumed if neither a command file nor -c is used. (When using Expectk, this option is specified as -interactive.) โญ

-- may be used to delimit the end of the options. this is useful if you want to pass an option-like argument to your script without it being interpreted by Expect. this can usefully be placed in the #! line to prevent any flag-like interpretation by Expect. for example, the following will leave the original arguments (including the script name) in the variable argv.

#!/usr/bin/expect --

note that the usual getopt(3) and execve(2) conventions must be observed when adding arguments to the #! line. โš ๏ธ

the file $exp_library/expect.rc is sourced automatically if present, unless the -N flag is used. (When using Expectk, this option is specified as -NORC.) immediately after this, the file ~/.expect.rc is sourced automatically, unless the -n flag is used. if the environment variable DOTDIR is defined, it is treated as a directory and .expect.rc is read from there. (When using Expectk, this option is specified as -norc.) this sourcing occurs only after executing any -c flags. ๐ŸŒฑ

-v causes Expect to print its version number and exit. (The corresponding flag in Expectk, which uses long flag names, is -version.) ๐Ÿ”–

optional args are constructed into a list and stored in the variable named argv. argc is initialized to the length of argv. ๐Ÿ“‹

argv0 is defined to be the name of the script (or binary if no script is used). for example, the following prints out the name of the script and the first three arguments:

send_user "$argv0 [lrange $argv 0 2]\n"

๐Ÿ‘‘ COMMANDS ๐Ÿ‘‘

Expect uses Tcl (Tool Command Language). Tcl provides control flow (e.g., if, for, break), expression evaluation and several other features such as recursion, procedure definition, etc. commands used here but not defined (e.g., set, if, exec) are Tcl commands (see tcl(3)). Expect supports additional commands, described below. unless otherwise specified, commands return the empty string. ๐Ÿซง

commands are listed alphabetically so that they can be quickly located. however, new users may find it easier to start by reading the descriptions of spawn, send, expect, and interact, in that order. that's the starter pack bestie, trust ๐ŸŽ€

note that the best introduction to the language (both Expect and Tcl) is provided in the book "Exploring Expect" (see SEE ALSO below). examples are included in this man page but they are very limited since this man page is meant primarily as reference material. ๐Ÿ“š

note that in the text of this man page, "Expect" with an uppercase "E" refers to the Expect program while "expect" with a lower-case "e" refers to the expect command within the Expect program. crucial distinction, don't mix them up ๐Ÿ’€


close [-slave] [-onexec 0|1] [-i spawn_id]

closes the connection to the current process. most interactive programs will detect EOF on their stdin and exit; thus close usually suffices to kill the process as well. the -i flag declares the process to close corresponding to the named spawn_id. โœ‚๏ธ

both expect and interact will detect when the current process exits and implicitly do a close. but if you kill the process by, say, "exec kill $pid", you will need to explicitly call close. ๐Ÿ’€

the -onexec flag determines whether the spawn id will be closed in any new spawned processes or if the process is overlayed. to leave a spawn id open, use the value 0. a non-zero integer value will force the spawn closed (the default) in any new processes. ๐Ÿšช

the -slave flag closes the slave associated with the spawn id. (See "spawn -pty".) when the connection is closed, the slave is automatically closed as well if still open. ๐Ÿ”—

no matter whether the connection is closed implicitly or explicitly, you should call wait to clear up the corresponding kernel process slot. close does not call wait since there is no guarantee that closing a process connection will cause it to exit. see wait below for more info. ๐Ÿงน

debug [[-now] 0|1]

controls a Tcl debugger allowing you to step through statements, set breakpoints, etc. ๐Ÿž

with no arguments, a 1 is returned if the debugger is not running, otherwise a 0 is returned. ๐Ÿค”

with a 1 argument, the debugger is started. with a 0 argument, the debugger is stopped. if a 1 argument is preceded by the -now flag, the debugger is started immediately (i.e., in the middle of the debug command itself). otherwise, the debugger is started with the next Tcl statement. โฑ๏ธ

the debug command does not change any traps. compare this to starting Expect with the -D flag (see above). ๐Ÿชค

see the README file or SEE ALSO (below) for more information on the debugger. ๐Ÿ“–

disconnect

disconnects a forked process from the terminal. it continues running in the background. the process is given its own process group (if possible). standard I/O is redirected to /dev/null. ๐ŸŒ‘โœŒ๏ธ

the following fragment uses disconnect to continue running the script in the background.

if {[fork]!=0} exit
disconnect
. . .

the following script reads a password, and then runs a program every hour that demands a password each time it is run. the script supplies the password so that you only have to type it once. (See the stty command which demonstrates how to turn off password echoing.)

send_user "password?\ "
expect_user -re "(.*)\n"
for {} 1 {} {
    if {[fork]!=0} {sleep 3600;continue}
    disconnect
    spawn priv_prog
    expect Password:
    send "$expect_out(1,string)\r"
    . . .
    exit
}

Okay so why is disconnect kinda iconic here? Because it beats the shell's async process thing (&) in one specific way: Expect can save your terminal parameters before it disconnects, and then re-apply them later to new ptys. With &, Expect never gets the chance to read the terminal's parameters, because by the time Expect gets control the terminal is already disconnected. Rip. ๐Ÿ’€


exit [-opts] [status]

This one makes Expect leave โ€” or at least get ready to leave. Farewell era. ๐Ÿ‘‹

  • -onexit โ€” the next argument gets used as an exit handler. With no argument at all, you get the current exit handler returned. Free real estate in a sense.
  • -noexit โ€” makes Expect prepare to exit but stop short of actually giving control back to the operating system. Your user-defined exit handler runs, and Expect's own internal handlers run too. But no further Expect commands should be executed. This is the move when you're running Expect alongside other Tcl extensions: the current interpreter (and the main window, if you're in the Tk environment) stays alive so those other Tcl extensions can clean up after themselves. And if Expect's exit gets called again (however that might happen), the handlers are not rerun. Noted, bestie. ๐Ÿ“

When it exits, all connections to spawned processes are closed. The spawned processes will detect that closure as an EOF. Beyond what a normal _exit(2) does, exit takes no other actions. Which means spawned processes that don't bother checking for EOF may straight up keep running. (A whole bunch of conditions matter for figuring out stuff like which signals a spawned process gets sent, but those are system-dependent and typically documented under exit(3).) Any spawned process that keeps running gets inherited by init.

status (or 0 if you didn't specify one) is returned as Expect's exit status. And heads up: exit is implicitly executed if the end of the script is reached. No escape. ๐Ÿซถ


exp_continue [-continue_timer]

exp_continue lets expect itself keep on going instead of returning like it normally would. By default, exp_continue resets the timeout timer. The -continue_timer flag stops the timer from being restarted. (Check out expect for the deets.)


exp_internal [-f file] value

Makes further commands send diagnostic info that's internal to Expect over to stderr, if value is non-zero. If value is 0, that output is disabled. The diagnostic info includes every character received, and every attempt made to match the current output against the patterns. It's giving surveillance but make it debugging. ๐Ÿ•ต๏ธโ€โ™€๏ธ

If you supply the optional file, all normal and debugging output gets written to that file (no matter what value is). Any previous diagnostic output file gets closed.

The -info flag makes exp_internal return a description of the most recent non-info arguments it was given.


exp_open [args] [-i spawn_id]

Returns a Tcl file identifier that corresponds to the original spawn id. That file identifier can then be used just like it was opened by Tcl's open command. (The spawn id should no longer be used. A wait should not be executed.)

The -leaveopen flag leaves the spawn id open for access through Expect commands โ€” and in that case a wait must be executed on the spawn id.


exp_pid [-i spawn_id]

Returns the process id corresponding to the currently spawned process. If you throw in the -i flag, the pid returned is the one matching the given spawn_id.


exp_send

Is an alias for send.

exp_send_error

Is an alias for send_error.

exp_send_log

Is an alias for send_log.

exp_send_tty

Is an alias for send_tty.

exp_send_user

Is an alias for send_user.


exp_version [[-exit] version]

This one's your little compatibility bestie โ€” it makes sure your script vibes with the version of Expect that's actually running. ๐Ÿ’…

With no arguments, you get back the current version of Expect. You can then bake that version straight into your script. And if you genuinely know you're not using any features from recent versions, you're allowed to specify an earlier version.

Versions are three numbers separated by dots:

  1. The major number first. Scripts written for versions of Expect with a different major number will almost certainly not work. exp_version returns an error if the major numbers don't match. ๐Ÿ˜ญ
  2. The minor number second. Scripts written for a version with a greater minor number than the current version might be depending on some new feature and might not run. exp_version returns an error if the major numbers match but the script's minor number is greater than the running Expect's.
  3. The third number plays no part in version comparison at all. It just gets incremented whenever the Expect software distribution changes in any way โ€” extra documentation, optimization, whatever. It's reset to 0 with each new minor version.

With the -exit flag, Expect prints an error and exits if the version is out of date. Dramatic exits only. โœจ


expect [[-opts] pat1 body1] ... [-opts] patn [bodyn]

The main character. ๐Ÿ‘‘

expect waits until one of the patterns matches the output of a spawned process, a specified time period has passed, or an end-of-file is seen. If the final body is empty, you can just omit it.

Patterns from the most recent expect_before command are implicitly used before any other patterns. Patterns from the most recent expect_after command are implicitly used after any other patterns.

If the arguments to the whole expect statement need more than one line, you can "brace" all the arguments into one so you don't have to end every line with a backslash. In this one specific case, the usual Tcl substitutions will still happen despite the braces.

  • If a pattern is the keyword eof, the corresponding body is executed on end-of-file.
  • If a pattern is the keyword timeout, the corresponding body is executed on timeout. If no timeout keyword is used, an implicit null action is executed on timeout. The default timeout period is 10 seconds, but you can set it โ€” to 30, say โ€” with the command set timeout 30. An infinite timeout can be designated with the value -1.
  • If a pattern is the keyword default, the corresponding body is executed on either timeout or end-of-file.

If a pattern matches, the corresponding body gets executed. expect returns the result of the body (or the empty string if no pattern matched). And if multiple patterns match, the one that appears first is the one used to select a body. First come, first served, no exceptions. ๐ŸŽ€

Every time new output arrives, it gets compared to each pattern in the order they're listed. So you can test for the absence of a match by making the last pattern something guaranteed to show up, like a prompt. In situations where there's no prompt, you have to use timeout โ€” exactly like you would if you were doing it manually.

Patterns come in three flavors:

  • By default, patterns are specified just like Tcl's string match command. (These are also similar to C-shell regular expressions, usually called "glob" patterns.) The -gl flag can protect patterns that would otherwise match expect flags from doing so. Any pattern that begins with a - should be protected this way. (All strings starting with - are reserved for future options. ๐Ÿ‘€)

For example, here's a fragment that looks for a successful login. (And yes, abort is presumed to be a procedure defined somewhere else in the script.)

expect {
    busy               {puts busy\n ; exp_continue}
    failed             abort
    "invalid password" abort
    timeout            abort
    connected
}

Quotes are necessary on that fourth pattern because it contains a space โ€” otherwise the space would separate the pattern from the action. Patterns that share the same action (like the 3rd and 4th here) require listing the actions again. You can dodge that by using regexp-style patterns (below). More info on forming glob-style patterns lives in the Tcl manual.

  • Regexp-style patterns follow the syntax defined by Tcl's regexp (short for "regular expression") command. Regexp patterns are introduced with the -re flag. The previous example can be rewritten with a regexp like this:
expect {
    busy       {puts busy\n ; exp_continue}
    -re "failed|invalid password" abort
    timeout    abort
    connected
}

Both kinds of patterns are "unanchored". That means patterns don't have to match the entire string โ€” they can begin and end the match anywhere in the string, as long as everything else matches. Use ^ to match the beginning of a string, and $ to match the end. And heads up: if you don't wait for the end of a string, your responses can easily end up smack in the middle of the string as they get echoed from the spawned process. The results are still correct, but the output can look unnatural. So using $ is encouraged whenever you can describe the characters at the end of a string exactly. ๐Ÿฆ‹

Now, in lots of editors, ^ and $ match the beginning and end of lines. But because expect is not line oriented, these characters match the beginning and end of the data (as opposed to lines) currently sitting in the expect matching buffer. (Also, peep the note below on "system indigestion".)

The -ex flag makes the pattern match as an "exact" string. No interpretation of *, ^, etc. is made (although the usual Tcl conventions still have to be observed). Exact patterns are always unanchored.

The -nocase flag makes uppercase characters of the output compare as if they were lowercase characters. The pattern itself is not affected.

okay so while it's reading output, if more than 2000 bytes have piled up, the earlier bytes can get straight-up "forgotten" ๐Ÿ’€ โ€” evicted, gone, no cap. You can change that limit with the match_max function. (Heads up though: excessively large values can slow down the pattern matcher, so don't go feral with it ๐Ÿ˜ญ.)

If patlist is full_buffer, the corresponding body gets executed when match_max bytes have been received and no other patterns have matched. And whether or not you used the full_buffer keyword, the forgotten characters still get written to expect_out(buffer). Nothing is ever truly lost, it's just been relocated ๐Ÿซถ.

If patlist is the keyword null, and nulls are allowed (that's a remove_nulls command thing), then the body runs when a single ASCII 0 gets matched. Quick reality check bestie: it is not possible to match 0 bytes via glob or regexp patterns. Not a vibe, not a skill issue, just not possible. ๐Ÿšซ


The moment a pattern matches (or eof or full_buffer happens), any matching output plus any previously unmatched output gets saved in the variable expect_out(buffer). Up to 9 regexp substring matches get saved in expect_out(1,string) through expect_out(9,string). If the -indices flag is used before a pattern, the starting and ending indices (in a form suitable for lrange) of the 10 strings get stored in the variables expect_out(X,start) and expect_out(X,end), where X is a digit corresponding to the substring position in the buffer. 0 refers to strings which matched the entire pattern โ€” and that's generated for glob patterns as well as regexp patterns, so everyone eats ๐Ÿ’….

For example, if a process has produced output of "abcdefgh\n", the result of:

expect "cd"

is as if the following statements had executed:

set expect_out(0,string) cd
set expect_out(buffer) abcd

and "efgh\n" is left in the output buffer. And if a process produced the output "abbbcabkkkka\n", the result of:

expect -indices -re "b(b*).*(k+)"

is as if the following statements had executed:

set expect_out(0,start) 1
set expect_out(0,end) 10
set expect_out(0,string) bbbcabkkkk
set expect_out(1,start) 2
set expect_out(1,end) 3
set expect_out(1,string) bb
set expect_out(2,start) 10
set expect_out(2,end) 10
set expect_out(2,string) k
set expect_out(buffer) abbbcabkkkk

and "a\n" is left in the output buffer. Also, the pattern "*" (and -re ".*") will flush the output buffer without reading any more output from the process. Iconic little trick, honestly.

Normally, the matched output is discarded from Expect's internal buffers. Want to keep it? Prefix a pattern with the -notransfer flag โ€” it's especially useful while you're experimenting. (And because you will be experimenting, it can be abbreviated to -not for convenience while experimenting ๐Ÿ˜Œ.)

The spawn id associated with the matching output (or eof or full_buffer) gets stored in expect_out(spawn_id).

The -timeout flag makes the current expect command use the following value as a timeout instead of using the value of the timeout variable. One-off timeout override, slay.


By default, patterns are matched against output from the current process. But the -i flag declares that output from the named spawn_id list gets matched against any following patterns (up to the next -i). That spawn_id list should either be a whitespace separated list of spawn_ids, or a variable referring to such a list of spawn_ids.

For example, this next one waits for "connected" from the current process, or "busy", "failed" or "invalid password" from the spawn_id named by $proc2:

expect {
    -i $proc2 busy {puts busy\n ; exp_continue}
    -re "failed|invalid password" abort
    timeout abort
    connected
}

The value of the global variable any_spawn_id may be used to match patterns to any spawn_ids that are named with all other -i flags in the current expect command. And a spawn_id from a -i flag with no associated pattern (i.e., followed immediately by another -i) gets made available to any other patterns in the same expect command associated with any_spawn_id.

The -i flag may also name a global variable โ€” in which case the variable is read for a list of spawn ids, and it's reread whenever it changes. This is how you change the I/O source while the command is in execution. Spawn ids provided this way are called "indirect" spawn ids.


Actions such as break and continue cause control structures (i.e., for, proc) to behave in the usual way. The command exp_continue lets expect itself keep executing rather than returning like it normally would.

This is useful for avoiding explicit loops or repeated expect statements. The following example is part of a fragment to automate rlogin โ€” the exp_continue means you don't have to write a second expect statement (to look for the prompt again) if rlogin prompts for a password:

expect {
    Password: {
        stty -echo
        send_user "password (for $user) on $host: "
        expect_user -re "(.*)\n"
        send_user "\n"
        send "$expect_out(1,string)\r"
        stty echo
        exp_continue
    } incorrect {
        send_user "invalid password or account\n"
        exit
    } timeout {
        send_user "connection to $host timed out\n"
        exit
    } eof {
        send_user \
            "connection to host failed: $expect_out(buffer)"
        exit
    } -re $prompt
}

Another example: this fragment might help a user guide an interaction that's already totally automated. The terminal gets put into raw mode. If the user presses "+", a variable is incremented. If "p" is pressed, several returns get sent to the process โ€” maybe to poke it in some way. And "i" lets the user interact with the process, effectively stealing away control from the script. In each case, the exp_continue lets the current expect continue pattern matching after executing the current action:

stty raw -echo
expect_after {
    -i $user_spawn_id
    "p" {send "\r\r\r"; exp_continue}
    "+" {incr foo; exp_continue}
    "i" {interact; exp_continue}
    "quit" exit
}

By default, exp_continue resets the timeout timer. The timer is not restarted if exp_continue is called with the -continue_timer flag.

expect_after [expect_args]

Works identically to the expect_before except that if patterns from both expect and expect_after can match, the expect pattern is used. See the expect_before command for more information. ๐Ÿซถ

expect_background [expect_args]

Takes the same arguments as expect, however it returns immediately. Patterns are tested whenever new input arrives. The pattern timeout and default are meaningless to expect_background and are silently discarded ๐Ÿ™ˆ. Otherwise, the expect_background command uses expect_before and expect_after patterns just like expect does.

When expect_background actions are being evaluated, background processing for the same spawn id is blocked. Background processing is unblocked when the action completes. While background processing is blocked, it is possible to do a (foreground) expect on the same spawn id.

Lowkey important: it is not possible to execute an expect while an expect_background is unblocked. ๐Ÿ˜ญ

expect_background for a particular spawn id is deleted by declaring a new expect_background with the same spawn id. Declaring expect_background with no pattern removes the given spawn id from the ability to match patterns in the background.

expect_before [expect_args]

Takes the same arguments as expect, however it returns immediately. Pattern-action pairs from the most recent expect_before with the same spawn id are implicitly added to any following expect commands. If a pattern matches, it's treated as if it had been specified in the expect command itself, and the associated body is executed in the context of the expect command. If patterns from both expect_before and expect can match, the expect_before pattern is used.

If no pattern is specified, the spawn id is not checked for any patterns.

Unless overridden by a -i flag, expect_before patterns match against the spawn id defined at the time that the expect_before command was executed (not when its pattern is matched). Subtle but so real.

The -info flag causes expect_before to return the current specifications of what patterns it will match. By default, it reports on the current spawn id. An optional spawn id specification may be given for information on that spawn id. For example:

expect_before -info -i $proc

At most one spawn id specification may be given. The flag -indirect suppresses direct spawn ids that come only from indirect specifications.

Instead of a spawn id specification, the flag "-all" will cause "-info" to report on all spawn ids.

The output of the -info flag can be reused as the argument to expect_before. Recycling queen ๐Ÿ‘‘

expect_tty [expect_args]

Is like expect but it reads characters from /dev/tty (i.e. keystrokes from the user). By default, reading is performed in cooked mode. So bestie โ€” lines must end with a return in order for expect to see them. This may be changed via stty (see the stty command below).

expect_user [expect_args]

Is like expect but it reads characters from stdin (i.e. keystrokes from the user). By default, reading is performed in cooked mode. Thus, lines must end with a return in order for expect to see them. This may be changed via stty (see the stty command below).

fork ๐Ÿด

okay so fork makes a whole new process, and bestie, it's literally a exact copy of the current Expect process. no changes, no notes, full clone energy ๐Ÿ’…

On success, fork returns 0 to the new (child) process and hands the parent the process ID of that child. On failure โ€” which is invariably due to lack of resources, like swap space or memory โ€” fork returns -1 to the parent, and no child process gets created at all. sad. ๐Ÿ˜ญ

Forked processes dip out via the exit command, just like the original process does. And yes, forked processes are allowed to write to the log files โ€” but if you don't disable debugging or logging in most of the processes, the result can be confusing. like, chaos. do not recommend.

โš ๏ธ Some pty implementations may be confused by multiple readers and writers, even momentarily. So the safest move is to fork before spawning processes. periodt.

interact ๐Ÿ’–

interact [string1 body1] ... [stringn [bodyn]]

This one hands control of the current process over to the user ๐Ÿซถ โ€” so your keystrokes get sent to the current process, and the current process's stdout and stderr come right back to you. it's giving conversation.

String-body pairs can be passed as arguments, and the body runs when the matching string gets entered. (By default, the string is not sent to the current process.) And if the final body is missing, the interpreter command is just assumed. iconic default behavior โœจ

If the arguments to the whole interact statement need more than one line, you can "brace" all the arguments into one so you don't have to backslash-terminate every single line. In this one specific case, the usual Tcl substitutions will occur despite the braces. iykyk.

For example, this command runs interact with all these string-body pairs defined: press ^Z and Expect gets suspended (the -reset flag restores the terminal modes). Press ^A and the user sees "you typed a control-A" and the process gets sent a ^A. Press $ and the user sees the date. Press ^C and Expect exits. Type "foo" and the user sees "bar". Press ~~ and the Expect interpreter runs interactively.

set CTRLZ \032
interact {
    -reset $CTRLZ {exec kill -STOP [pid]}
    \001   {send_user "you typed a control-A\n";
            send "\001"
           }
    $      {send_user "The date is [clock format [clock seconds]]."}
    \003   exit
    foo    {send_user "bar"}
    ~~
}

In string-body pairs, strings get matched in the order they're listed as arguments. Strings that only partially match are not sent to the current process, because everyone's waiting to see if the remainder shows up. But if characters are then entered such that a match can no longer possibly happen, only the part of the string gets sent to the process โ€” the part that can't possibly begin another match. So strings that are substrings of partial matches can still match later, if the original string that was trying to be matched ultimately fails. it's giving string detective work ๐Ÿ”

By default, string matching is exact with no wild cards. (In contrast, the expect command uses glob-style patterns by default.) The -ex flag can protect patterns that might otherwise match interact flags from doing so โ€” any pattern beginning with a - should be protected this way. (All strings starting with - are reserved for future options.)

The -re flag forces the string to be interpreted as a regexp-style pattern. In that case, matching substrings get stored in the variable interact_out, similarly to the way expect stores its output in the variable expect_out. The -indices flag is similarly supported. ๐Ÿ‘‘

The pattern eof introduces an action that runs upon end-of-file. A separate eof pattern may also follow the -output flag, in which case it's matched if an eof is detected while writing output. The default eof action is "return", so interact simply returns upon any EOF. clean. โœจ

The pattern timeout introduces a timeout (in seconds) and an action executed after no characters have been read for a given time. The timeout pattern applies to the most recently specified process. There is no default timeout. And lowkey important: the special variable "timeout" (used by the expect command) has no affect on this timeout.

For example, this statement could be used to autologout users who haven't typed anything for an hour but who still get frequent system messages:

interact -input $user_spawn_id timeout 3600 return -output \
    $spawn_id

If the pattern is the keyword null, and nulls are allowed (via the remove_nulls command), the corresponding body is executed if a single ASCII 0 is matched. Hmm, and ngl: it is not possible to match 0 bytes via glob or regexp patterns. noted. ๐Ÿ“

Prefacing a pattern with the flag -iwrite causes the variable interact_out(spawn_id) to be set to the spawn_id which matched the pattern (or eof).

Actions like break and continue cause control structures (i.e., for, proc) to behave in the usual way. However, return causes interact to return to its caller, while inter_return causes interact to cause a return in its caller. So for example, if "proc foo" called interact which then executed the action inter_return, proc foo would return. (This means that if interact calls interpreter and you interactively type return, the interact will continue โ€” while inter_return will cause the interact to return to its caller.) the way that naming works here... ๐Ÿ’€

During interact, raw mode is used so that all characters may be passed to the current process. If the current process doesn't catch job control signals, it will stop if it's sent a stop signal (by default ^Z). To restart it, send a continue signal (such as by "kill -CONT <pid>"). If you really want to send a SIGSTOP to such a process (by ^Z), consider spawning csh first and then running your program. On the other hand, if you want to send a SIGSTOP to Expect itself, first call interpreter (maybe using an escape character), and then press ^Z.

String-body pairs can be a cute shorthand so you don't have to enter the interpreter and run commands interactively. The previous terminal mode is used while the body of a string-body pair is executing.

For speed, actions execute in raw mode by default. The -reset flag resets the terminal to the mode it had before interact was executed (invariably, cooked mode). Note that characters entered while the mode is being switched may be lost โ€” an unfortunate feature of the terminal driver on some systems ๐Ÿฅฒ. The only reason to use -reset is if your action depends on running in cooked mode.

The -echo flag sends characters that match the following pattern back to the process that generated them, as each character is read. This can be useful when the user needs to see feedback from partially typed patterns.

If a pattern is being echoed but eventually fails to match, the characters get sent to the spawned process. And if the spawned process then echoes them, the user will see the characters twice. so -echo is probably only appropriate in situations where the user is unlikely to not complete the pattern. For example, this excerpt is from rftp, the recursive-ftp script, where the user is prompted to enter ~g, ~p, or ~l, to get, put, or list the current directory recursively. These are so far away from the normal ftp commands that the user is unlikely to type ~ followed by anything else โ€” except mistakenly, in which case they'll probably just ignore the result anyway. ๐Ÿ™ˆ

interact {
    -echo ~g {getcurdirectory 1}
    -echo ~l {getcurdirectory 0}
    -echo ~p {putcurdirectory}
}

The -nobuffer flag sends characters that match the following pattern on to the output process as characters are read.

This is useful when you want to let a program echo back the pattern. For example, this might be used to monitor where a person is dialing (a Hayes-style modem). Each time "atd" is seen, the script logs the rest of the line. โญ

proc lognumber {} {
    interact -nobuffer -re "(.*)\r" return
    puts $log "[clock format [clock seconds]]: dialed $interact_out(1,string)"
}

interact -nobuffer "atd" lognumber

During interact, previous use of log_user is ignored. In particular, interact will force its output to be logged (sent to the standard output), since it's presumed the user doesn't wish to interact blindly. ๐Ÿ’…

The -o flag causes any following key-body pairs to be applied to the output of the current process. This can be useful, for example, when dealing with hosts that send unwanted characters during a telnet session.

By default, interact expects the user to be writing stdin and reading stdout of the Expect process itself. The -u flag (for "user") makes interact look for the user as the process named by its argument (which must be a spawned id).

This lets two unrelated processes be joined together without an explicit loop. To aid in debugging, Expect diagnostics always go to stderr (or stdout for certain logging and debugging information). For the same reason, the interpreter command will read interactively from stdin.

For example, this fragment creates a login process. Then it dials the user (not shown), and finally connects the two together. Of course, any process may be substituted for login โ€” a shell, for example, would let the user work without supplying an account and password. ๐ŸŒˆ

spawn login
set login $spawn_id
spawn tip modem
# dial back out to user
# connect user to login
interact -u $login

To send output to multiple processes at once, just list each spawn id list with a -output flag in front of it. And the input for a group of output spawn ids? That gets decided by a spawn id list prefaced with -input. Both -input and -output take lists in the exact same form as the -i flag in the expect command โ€” with one little exception, bestie: any_spawn_id is not meaningful in interact. Sorry.

Every following flag and string (or pattern) applies to that input until another -input flag shows up. No -input at all? Then -output implies -input $user_spawn_id -output. (Same deal with patterns that don't have an -input.) If one -input is specified, it overrides $user_spawn_id. If a second -input is specified, it overrides $spawn_id. And yes, you can keep stacking additional -input flags. Slay.

The two implied input processes default to having their outputs specified as $spawn_id and $user_spawn_id (in reverse). And if a -input flag appears with no -output flag? Characters from that process get yeeted โ€” discarded. ๐Ÿ’€

The -i flag brings in a replacement for the current spawn_id when no other -input or -output flags are used, and a -i flag implies an -o flag. You can also switch up which processes you're interacting with using indirect spawn ids ๐Ÿ‘€ (they're described in the section on the expect command). Indirect spawn ids can be specified with the -i, -u, -input, or -output flags. The flexibility?? Iconic.


โœจ interpreter [args] โœจ

This one turns you into the prompt. Literally โ€” it causes the user to be interactively prompted for Expect and Tcl commands, and the result of each command gets printed. Very main character of it.

  • Actions like break and continue make control structures (i.e., for, proc) do their usual thing, no drama.
  • But return causes interpreter to return to its caller, while inter_return makes interpreter cause a return in its caller. So like, if proc foo called interpreter which then executed the action inter_return, then proc foo would return. That's the whole bit.
  • Any other command just makes interpreter keep prompting for new commands. Endless loop energy ๐Ÿ”

By default the prompt has two integers in it. The first integer describes the depth of the evaluation stack (i.e., how many times Tcl_Eval has been called ๐Ÿซ ). The second integer is the Tcl history identifier. Wanna customize? Define a procedure called prompt1 โ€” its return value becomes the next prompt. And if a statement has open quotes, parens, braces, or brackets, a secondary prompt (default "+> ") shows up on newline. That one's controlled by defining a procedure called prompt2.

During interpreter, cooked mode is used โ€” even if its caller was in raw mode. Consistency, we love to see it. โœจ

Heads up bestie: if stdin is closed, interpreter will return, unless the -eof flag is used, in which case the subsequent argument is invoked.

๐ŸŽ€ log_file [args] [[-a] file] ๐ŸŽ€

Give it a filename and log_file will record a transcript of the session (starting from that exact point) into that file. Give it no argument and it stops recording. Any previous log file gets closed. Simple as.

Instead of a filename, you can hand it a Tcl file identifier using the -open or -leaveopen flags. It's similar to the spawn command โ€” (see spawn for more info, iykyk).

  • -a โ€” forces output to be logged that was suppressed by the log_user command.
  • -noappend โ€” by default, log_file appends to old files rather than truncating them, because it's convenient to be able to turn logging off and on multiple times in one session. If you want truncation instead, this is your flag.
  • -info โ€” makes log_file return a description of the most recent non-info arguments given.

๐ŸŒธ log_user -info|0|1 ๐ŸŒธ

By default, the send/expect dialogue is logged to stdout (and to a logfile, if one's open). The stdout logging is disabled by log_user 0 and re-enabled by log_user 1. Logging to the logfile stays unchanged, no matter what you do here.

  • -info โ€” makes log_user return a description of the most recent non-info arguments given.

๐Ÿฆ‹ match_max [-d] [-i spawn_id] [size] ๐Ÿฆ‹

This defines the size of the buffer (in bytes) that expect uses internally. No size argument? It just returns the current size at you.

  • -d โ€” sets the default size. (The initial default is 2000.)
  • -i โ€” sets the size for the named spawn id. No -i? Then it's set for the current process.

๐Ÿ‘‘ overlay [-# spawn_id] [-# spawn_id] [...] program [args] ๐Ÿ‘‘

This executes program args in place of the current Expect program, which then terminates. Bye bestie ๐Ÿซถ

A bare hyphen argument forces a hyphen in front of the command name, as if it was a login shell. Every spawn_id gets closed except for the ones named as arguments โ€” those get mapped onto the named file identifiers. Spawn_ids are mapped to file identifiers so the new program can inherit them.

For example, this line runs chess and lets it be controlled by the current process โ€” say, a chess master:

overlay -0 $spawn_id -1 $spawn_id -2 $spawn_id chess

This is more efficient than interact -u, however, it sacrifices the ability to do programmed interaction, since the Expect process is no longer in control. Trade-offs, always.

Note that no controlling terminal is provided. So if you disconnect or remap standard input, programs that do job control (shells, login, etc) will not function properly. Not a vibe.

๐Ÿ’œ parity [-d] [-i spawn_id] [value] ๐Ÿ’œ

Defines whether parity should be retained or stripped from the output of spawned processes. If value is zero, parity is stripped; otherwise it is not stripped. No value argument? The current value gets returned.

  • -d โ€” sets the default parity value. (The initial default is 1, i.e., parity is not stripped.)
  • -i โ€” sets the parity value for the named spawn id; otherwise it's set for the current process.

๐Ÿ’… remove_nulls [-d] [-i spawn_id] [value] ๐Ÿ’…

Defines whether nulls are retained or removed from the output of spawned processes โ€” and this happens before pattern matching or storing in the variable expect_out or interact_out, so it actually matters.

  • value of 1 โ†’ nulls are removed.
  • value of 0 โ†’ nulls are not removed.
  • No value argument? The current value is returned.
  • -d โ€” sets the default value. (The initial default is 1, i.e., nulls are removed.)
  • -i โ€” sets the value for the named spawn id; otherwise it's set for the current process.
Oh and โ€” whether or not nulls are removed, Expect will still record null bytes to the log and stdout. So like, don't think you escaped them.

โญ send [-flags] string โญ

Sends string to the current process. For example, the command

send "hello world\r"

sends the characters, h e l l o <blank> w o r l d <return> to the current process. (Tcl includes a printf-like command called format, which can build arbitrarily complex strings โ€” very handy.)

Characters are sent immediately, although programs with line-buffered input will not read the characters until a return character is sent. A return character is denoted "\r".

Now the flags, bestie, brace yourself:

  • -- โ€” forces the next argument to be interpreted as a string rather than a flag. Any string can be preceded by -- whether or not it actually looks like a flag. This gives you a reliable mechanism to specify variable strings without getting tripped up by the ones that accidentally look like flags. (All strings starting with - are reserved for future options. ๐Ÿ‘€)
  • -i โ€” declares that the string be sent to the named spawn_id. If the spawn id is user_spawn_id and the terminal is in raw mode, newlines in the string are translated to return-newline sequences, so that they appear as if the terminal was in cooked mode. The -raw flag disables this translation.
  • -null โ€” sends null characters (0 bytes). By default, one null is sent. An integer may follow the -null to indicate how many nulls to send.
  • -break โ€” generates a break condition. This only makes sense if the spawn id refers to a tty device opened via spawn -open. And if you've spawned a process such as tip, you should use tip's convention for generating a break.
  • -s โ€” forces output to be sent "slowly", which avoids that super common situation where a computer outtypes an input buffer that was designed for a human who would never outtype the same buffer. This output is controlled by the value of the variable send_slow, which takes a two element list. The first element is an integer describing the number of bytes to send atomically. The second element is a real number describing the number of seconds by which the atomic sends must be separated. For example, set send_slow {10 .001} would force send -s to send strings with 1 millisecond in between each 10 characters sent.
  • -h โ€” forces output to be sent (somewhat) like a human actually typing. Human-like delays appear between the characters. (The algorithm is based upon a Weibull distribution, with modifications to suit this particular application.) This output is controlled by the value of the variable send_human, which takes a five element list:
    1. The first two elements are average interarrival time of characters in seconds. The first is used by default.
    2. The second is used at word endings, to simulate the subtle pauses that occasionally occur at such transitions.
    3. The third parameter is a measure of variability, where .1 is quite variable, 1 is reasonably variable, and 10 is quite invariable. The extremes are 0 to infinity.
    4. The last two parameters are, respectively, a minimum and maximum interarrival time. The minimum and maximum are used last and "clip" the final time.

The ultimate average can be quite different from the given average if the minimum and maximum clip enough values. Math is wild like that.

As an example, the following command emulates a fast and consistent typist:

set send_human {.1 .3 1 .05 2}
send -h "I'm hungry.  Let's do lunch."

while the following might be more suitable after a hangover:

set send_human {.4 .4 .2 .5 100}
send -h "Goodd party lash night!"
Note that errors are not simulated โ€” although you can set up error correction situations yourself by embedding mistakes and corrections in a send argument. Manual chaos, but make it cute. ๐Ÿ™ˆ

The flags for blasting out null characters, for sending breaks, for forcing slow output and for that human-typed vibe are mutually exclusive bestie โ€” only the one you specified last is the one that actually runs. And also: you literally cannot hand a string argument to the flags for sending null characters or breaks. Not a thing.

Heads up ๐Ÿซถ it's a really good idea to hit a process with an expect before your first send. expect will wait for the process to actually start, whereas send simply cannot do that. And if that first send finishes before the process is even running?? yeah your data just gets ignored, no cap. So for interactive programs that give you no initial prompt, you can just put a delay before the send, like this:
# To avoid giving hackers hints on how to break in,
# this system does not prompt for an external password.
# Wait for 5 seconds for exec to complete
spawn telnet very.secure.gov
sleep 5
send password\r

exp_send is an alias for send. Important psa: if you're in Expectk or some other Tk-environment variant of Expect, send is defined by Tk for a totally different purpose ๐Ÿ™ˆ. So exp_send exists for compatibility between environments. And similar aliases exist for Expect's other send commands too.


  • send_error [-flags] string โ€” literally send but the output goes to stderr instead of the current process.
  • send_log [--] string โ€” like send but the string only goes to the log file (peep log_file). If no log file is open, the arguments just get ignored. ๐Ÿคท
  • send_tty [-flags] string โ€” same as send but the output goes to /dev/tty instead of the current process.
  • send_user [-flags] string โ€” same as send but the output goes to stdout instead of the current process.
  • sleep seconds โ€” makes the script nap for the given number of seconds. And yes, seconds can be a decimal number, we love precision โœจ. Interrupts (and Tk events, if you're on Expectk) still get processed while Expect is snoozing.
  • spawn [args] program [args] โ€” creates a whole new process running program args. Its stdin, stdout and stderr all get connected to Expect so other Expect commands can read from and write to them. The connection breaks on close, or if the process itself closes any of those file identifiers.

When a process is started by spawn, the variable spawn_id gets set to a descriptor pointing at that process. Whatever process spawn_id describes is considered the current process. And surprise โ€” spawn_id can be read or written, which is basically job control, iykyk. ๐Ÿ‘‘

user_spawn_id is a global variable holding a descriptor that refers to the user. So like, when spawn_id is set to that value, expect behaves like expect_user.

error_spawn_id is a global variable holding a descriptor that refers to standard error. So when spawn_id is set to that value, send behaves like send_error.

tty_spawn_id is a global variable holding a descriptor referring to /dev/tty. If /dev/tty doesn't exist (like in a cron, at, or batch script), then tty_spawn_id is not defined. You can test for it like:

if {[info vars tty_spawn_id]} {
    # /dev/tty exists
} else {
    # /dev/tty doesn't exist
    # probably in cron, batch, or at script
}

spawn returns the UNIX process id. If nothing got spawned, it returns 0. The variable spawn_out(slave,name) gets set to the name of the pty slave device.

By default spawn echoes the command name and arguments. The -noecho flag stops it from doing that.

The -console flag redirects console output to the spawned process. This is not supported on all systems, so don't come crying to me, bestie.

Internally, spawn uses a pty, initialized the same way as the user's tty. Then it's further initialized so all settings are "sane" (according to stty(1)). If the variable stty_init is defined, it gets interpreted in the style of stty arguments as extra config. For example, set stty_init raw makes future spawned processes' terminals start in raw mode. -nottycopy skips the initialization based on the user's tty. -nottyinit skips the "sane" initialization.

Normally spawn is quick as hell. But if you notice it taking a significant amount of time, it's probably hitting ptys that are wedged. Expect runs a bunch of tests on ptys to dodge entanglements with errant processes. (These take 10 seconds per wedged pty, ouch ๐Ÿ˜ญ.) Running Expect with the -d option will show if Expect is encountering lots of ptys in odd states. And if you can't kill the processes those ptys are attached toโ€ฆ your only recourse may just be to reboot. Yeah. Fr fr.

If program can't be spawned successfully because exec(2) fails (e.g. when program doesn't exist), an error message gets returned by the next interact or expect command โ€” as if program had run and produced that error message as output. This is just a natural consequence of how spawn is implemented, bestie. Internally spawn forks, and after that the spawned process has no way to communicate with the original Expect process except via the spawn_id.

The -open flag makes the next argument get interpreted as a Tcl file identifier (i.e., something returned by open). Then the spawn id can be used as if it were a spawned process. (And the file identifier should no longer be used, okay?!) This lets you treat raw devices, files and pipelines as spawned processes without touching a pty. 0 is returned to indicate there's no associated process. When the connection to the spawned process closes, so does the Tcl file identifier. The -leaveopen flag is like -open except it leaves the file identifier open even after the spawn id is closed. ๐Ÿ’…

The -pty flag opens a pty but spawns no process. 0 is returned to indicate there's no associated process. Spawn_id is set as usual.

The variable spawn_out(slave,fd) gets set to a file identifier corresponding to the pty slave. It can be closed using "close -slave".

The -ignore flag names a signal to be ignored in the spawned process. Otherwise, signals get the default behavior. Signals are named as in the trap command, except that each signal requires a separate flag.

  • strace level โ€” makes the following statements get printed before they're executed. (Tcl's trace command traces variables, this one's different.) level says how far down the call stack to trace. For example, this command runs Expect while tracing the first 4 levels of calls, but nothing below that:
expect -c "strace 4" script.exp

The -info flag makes strace return a description of the most recent non-info arguments given.

  • stty args โ€” changes terminal modes, similar to the external stty command.

By default it accesses the controlling terminal. Other terminals can be accessed by appending "< /dev/tty..." to the command. (Note that the arguments should not be grouped into a single argument.)

Requests for status return it as the result of the command. If no status is requested and the controlling terminal is accessed, the previous status of the raw and echo attributes are returned in a form that can be used later by the command.

For example, the arguments raw or -cooked put the terminal into raw mode. The arguments -raw or cooked put the terminal into cooked mode. The arguments echo and -echo put the terminal into echo and noecho mode respectively.

Here's an example showing how to temporarily disable echoing โ€” which you could use in otherwise-automatic scripts to avoid embedding passwords in them. (There's more discussion on this under EXPECT HINTS below.) ๐Ÿ‘€

stty -echo
send_user "Password: "
expect_user -re "(.*)\n"
set password $expect_out(1,string)
stty echo
  • system args โ€” hands args to sh(1) as input, just like it'd been typed as a command from a terminal. Expect waits until the shell terminates. The return status from sh is handled the same way exec handles its return status.

Unlike exec, which redirects stdin and stdout to the script, system performs no redirection (other than whatever the string itself says). So it's totally possible to use programs that must talk directly to /dev/tty. And for that same reason, system's results are not recorded in the log.

  • timestamp [args] โ€” returns a timestamp. With no arguments, you get the number of seconds since the epoch. โฐ

The -format flag introduces a string which is returned but with substitutions made according to the POSIX rules for strftime. For example %a gets replaced by an abbreviated weekday name (i.e., Sat). The others are:

%a      abbreviated weekday name
%A      full weekday name
%b      abbreviated month name
%B      full month name
%c      date-time as in: Wed Oct  6 11:45:56 1993
%d      day of the month (01โ€“31)
%H      hour (00โ€“23)
%I      hour (01โ€“12)
%j      day (001โ€“366)
%m      month (01โ€“12)
%M      minute (00โ€“59)
%p      am or pm
%S      second (00โ€“61)
%u      day (1โ€“7, Monday is first day of week)
%U      week (00โ€“53, first Sunday is first day of week one)
%V      week (01โ€“53, ISO 8601 style)
%w      day (0โ€“6)
%W      week (00โ€“53, first Monday is first day of week one)
%x      date-time as in: Wed Oct  6 1993
%X      time as in: 23:59:59
%y      year (00โ€“99)
%Y      year as in: 1993
%Z      timezone (or nothing if not determinable)
%%      a bare percent sign

Other % specifications are undefined โ€” so don't get creative, bestie. Other characters get passed through untouched. And only the C locale is supported. ๐Ÿ’œ

The -seconds flag lets you hand over a number of seconds since the epoch and use, like, that as the source to format from. Otherwise? Current time, bestie, we're living in the now. ๐Ÿ’…

The -gmt flag forces timestamp output to use the GMT timezone. No flag? Local timezone it is, no cap.

  • trap [[command] signals] โ€” okay so this one makes the given command run whenever any of the given signals show up later. And the command runs in the global scope, important. If command is MIA, the signal action gets returned instead. If command is the string SIG_IGN, the signals are just ignored (it's giving "not my problem"). If command is the string SIG_DFL, the signals get reset to the system default. And signals? Either one lonely signal or a whole list of them. You can write signals numerically or symbolically, per signal(3), and the "SIG" prefix is totally optional โ€” leave it at home, she's flexible like that. ๐ŸŽ€

With no arguments at all (or with the argument -number), trap returns the signal number of the trap command that's currently executing. Very meta, very main character.

  • -code โ€” uses the return code of the command instead of whatever code Tcl was about to return when the command originally started running. (Tcl was going to say something, but this flag is like "no, we're using yours".)
  • -interp โ€” evaluates the command using the interpreter that was active when the command started running, not whenever the trap got declared. Time travel but make it interpreters. โœจ
  • -name โ€” makes trap return the signal name of the trap command currently executing.
  • -max โ€” makes trap return the largest signal number that can even be set. The ceiling, fr.

So like, for example, the command trap {send_user "Ouch!"} SIGINT will print Ouch! every single time the user presses ^C. Someone's getting poked and they're being very vocal about it. ๐Ÿ˜ญ

By default, SIGINT (the one you usually get from pressing ^C) and SIGTERM cause Expect to exit. Why? Because of this trap right here, which gets created by default the moment Expect starts:

trap exit {SIGINT SIGTERM}

But if you use the -D flag to start the debugger, SIGINT gets redefined to start up the interactive debugger instead, thanks to this trap:

trap {exp_debug 1} SIGINT

And you can swap that debugger trap out by setting the environment variable EXPECT_DEBUG_INIT to a new trap command. Customization, we love to see it. ๐Ÿฆ‹

You can absolutely override both of these just by adding your own trap commands to your script. Specifically: if you have your own trap exit SIGINT, that overrides the debugger trap. Which is genuinely useful if you want to keep users from ever reaching the debugger at all. Locked out, periodt. ๐Ÿ”’

If you want to define your own trap on SIGINT but still trap into the debugger when the debugger is running, do this:

if {![exp_debug]} {trap mystuff SIGINT}

Or alternatively, you can trap to the debugger using some other signal entirely. Pick your fighter. ๐Ÿ’œ

trap will not let you override the action for SIGALRM, because Expect uses that internally, it's spoken for. The disconnect command sets SIGALRM to SIG_IGN (ignore). You can reenable it, but only as long as you disable it again during any subsequent spawn commands. Complicated? Yes. Doable? Also yes.

See signal(3) for more info.

  • wait [args] โ€” this one delays until a spawned process terminates (or the current process, if none is named).

wait normally returns a list of four integers, and each one has a job:

  1. The first integer? The pid of the process that was waited upon.
  2. The second integer is the corresponding spawn id.
  3. The third integer is -1 if an operating system error occurred, or 0 otherwise.
  4. If the third integer was 0, the fourth integer is the status returned by the spawned process. If the third integer was -1, then the fourth integer is the value of errno set by the operating system.

Also the global variable errorCode gets set. Every time. She's thorough like that. ๐Ÿ’…

Extra elements can show up at the end of the return value from wait, btw. An optional fifth element identifies a class of information. Right now the only possible value for that element is CHILDKILLED, in which case the next two values are the C-style signal name and a short textual description. Surprise bonus content. โœจ

  • -i โ€” declares the process to wait for by the named spawn_id (and NOT the process id, don't get it twisted). Inside a SIGCHLD handler, you can wait for any spawned process by using the spawn id -1.
  • -nowait โ€” makes the wait return immediately, with the indication of a successful wait. Then when the process exits later, it automatically disappears without you needing to do an explicit wait. Poof. Gone. Self-cleaning. ๐Ÿงš

The wait command can also wait for a forked process by using the arguments -i -1. And unlike when you use it with spawned processes, this one can be executed at literally any time. There's no control over which process gets reaped, though. However, you can check the return value for the process id. So like, you get the info, just not the steering wheel. ๐Ÿ™ˆ


๐Ÿ“š LIBRARIES ๐Ÿ“š

Expect automatically knows about two built-in libraries for Expect scripts. They're defined by the directories named in the variables exp_library and exp_exec_library. Both are meant to hold utility files that other scripts can use. Cute little shared resource moment. ๐Ÿซถ

exp_library contains architecture-independent files. exp_exec_library contains architecture-dependent files. And depending on your system, both directories might be completely, totally empty. Don't panic, that's normal. ๐Ÿ˜Œ

The existence of the file $exp_exec_library/cat-buffers describes whether your /bin/cat buffers by default. Yes, a whole file's existence doing all that work. Iconic.


๐ŸŽจ PRETTY-PRINTING ๐ŸŽจ

A vgrind definition is available for pretty-printing Expect scripts. So assuming the vgrind definition that ships with the Expect distribution is correctly installed, you can use it like this:

vgrind -lexpect file

Look at that output, she's clean. ๐Ÿ’–


๐ŸŒŸ EXAMPLES ๐ŸŒŸ

ngl it might not be obvious how to put everything together that this man page describes โ€” there's a lot going on. So I encourage you (yes, you) to read and try out the examples in the example directory of the Expect distribution. Some of them are real, actual programs. Others are just illustrative of certain techniques. And of course, a couple of them are just quick hacks. No shame in a quick hack. ๐Ÿ’€

The INSTALL file has a quick overview of these programs, so start there if you're feeling lost.

The Expect papers (see SEE ALSO) are also useful. Some of the papers use syntax corresponding to earlier versions of Expect, but the accompanying rationales are still valid and they go into a lot more detail than this man page does. Like, a lot a lot. ๐Ÿ“–


โš ๏ธ CAVEATS โš ๏ธ

Extensions may collide with Expect's command names. For example, send is defined by Tk for an entirely different purpose. Rude, honestly. For this reason, most of the Expect commands are also available as "exp_XXXX". But heads up: commands and variables beginning with "exp", "inter", "spawn", and "timeout" do not have aliases. So use the extended command names if you need this compatibility between environments. iykyk. ๐Ÿ‘‘

Expect takes a rather liberal view of scoping. Specifically, variables read by commands specific to the Expect program get sought first from the local scope, and if they're not found, then from the global scope. For example, this obviates the need to place "global timeout" in every procedure you write that uses expect. Handy! โœจ

On the other hand, variables written are always in the local scope โ€” unless a "global" command has been issued. And the most common problem this causes? When spawn is executed inside a procedure. Outside the procedure, spawn_id no longer exists, so the spawned process is no longer accessible, purely because of scoping. The fix: add a "global spawn_id" to that procedure. Write it down, bestie, this one bites people constantly. ๐Ÿ˜ญ

If you can't enable the multispawning capability (i.e., your system supports neither select (BSD *.*), poll (SVR>2), nor something equivalent), then Expect will only be able to control a single process at a time. In this case: do not attempt to set spawn_id, and you should not execute processes via exec while a spawned process is running. Furthermore, you will not be able to expect from multiple processes (including the user as one) at the same time. One at a time, that's the vibe. ๐Ÿข

Terminal parameters can have a huge effect on scripts. For example, if a script is written to look for echoing, it will misbehave if echoing gets turned off. For this reason, Expect forces sane terminal parameters by default. Unfortunately, this can make things unpleasant for other programs. As an example: the emacs shell wants to change the "usual" mappings โ€” newlines get mapped to newlines instead of carriage-return newlines, and echoing is disabled, which lets you use emacs to edit the input line. Unfortunately, Expect cannot possibly guess this. She's good but she's not a mind reader. ๐Ÿ™ˆ

You can request that Expect not override its default setting of terminal parameters. But you must then be very careful when writing scripts for such environments. In the case of emacs, avoid depending on things like echoing and end-of-line mappings.

Okay warning time, pay attention bestie: the commands that accepted arguments braced into a single list (the expect variants and interact) use a heuristic to decide if the list is actually one argument or many. The heuristic can fail only in the case when the list actually does represent a single argument which has multiple embedded \n's with non-whitespace characters between them. This seems sufficiently improbable, however the argument "-nobrace" can be used to force a single argument to be handled as a single argument. This could conceivably be used with machine-generated Expect code. Similarly, -brace forces a single argument to be handled as multiple patterns/actions. Two flags, opposite jobs, both here to save you. ๐Ÿ’…

๐Ÿ’€ BUGS ๐Ÿ’€ (aka the chaotic tea, no filter)

okay bestie, gather round, because this section is literally a list of โ€œthe vibes were offโ€ moments. no cap. hereโ€™s everything.

First: it was really tempting to name the program sex (for either โ€œSmart EXecโ€ or โ€œSend-EXpectโ€), but good sense (or maybe just Puritanism) prevailed. iconic restraint, honestly. ๐Ÿ’…

On some systems, when a shell is spawned, it complains about not being able to access the tty but runs anyway. This means your system has a mechanism for gaining the controlling tty that Expect doesnโ€™t know about. Please find out what it is, and send this information back to me. not me asking you to be the detective, but also yes. ๐Ÿ•ต๏ธโ€โ™€๏ธ

Ultrix 4.1 (at least the latest versions around here) considers timeouts of above 1000000 to be equivalent to 0. okay thatโ€™s a choice, Ultrix. ๐Ÿ˜ญ

Digital UNIX 4.0A (and probably other versions) refuses to allocate ptys if you define a SIGCHLD handler. See grantpt page for more info. go read that one, bestie.

IRIX 6.0 does not handle pty permissions correctly so that if Expect attempts to allocate a pty previously used by someone else, it fails. Upgrade to IRIX 6.1. periodt. ๐Ÿ‘‘

Telnet (verified only under SunOS 4.1.2) hangs if TERM is not set. This is a problem under cron, at and in cgi scripts, which do not define TERM. Thus, you must set it explicitly โ€“ to what type is usually irrelevant. It just has to be set to something! The following probably suffices for most cases.

set env(TERM) vt100

Tip (verified only under BSDI BSD/OS 3.1 i386) hangs if SHELL and HOME are not set. This is a problem under cron, at and in cgi scripts, which do not define these environment variables. Thus, you must set them explicitly โ€“ to what type is usually irrelevant. It just has to be set to something! The following probably suffices for most cases.

set env(SHELL) /bin/sh
set env(HOME) /usr/bin

Some implementations of ptys are designed so that the kernel throws away any unread output after 10 to 15 seconds (actual number is implementation-dependent) after the process has closed the file descriptor. Thus Expect programs such as

spawn date
sleep 20
expect

will fail. To avoid this, invoke non-interactive programs with exec rather than spawn. While such situations are conceivable, in practice I have never encountered a situation in which the final output of a truly interactive program would be lost due to this behavior. okay so donโ€™t panic, but do know. ๐ŸŒธ

On the other hand, Cray UNICOS ptys throw away any unread output immediately after the process has closed the file descriptor. I have reported this to Cray and they are working on a fix. ๐Ÿ’€

Sometimes a delay is required between a prompt and a response, such as when a tty interface is changing UART settings or matching baud rates by looking for start/stop bits. Usually, all this requires is to sleep for a second or two. A more robust technique is to retry until the hardware is ready to receive input. The following example uses both strategies:

send "speed 9600\r";
sleep 1
expect {
    timeout {send "\r"; exp_continue}
    $prompt
}

trap -code will not work with any command that sits in Tclโ€™s event loop, such as sleep. The problem is that in the event loop, Tcl discards the return codes from async event handlers. A workaround is to set a flag in the trap code. Then check the flag immediately after the command (i.e., sleep). okay so no cap, thatโ€™s the workaround.

The expect_background command ignores -timeout arguments and has no concept of timeouts in general. not it ignoring your whole timeout vibe, but yes.


๐Ÿ”ฎ EXPECT HINTS ๐Ÿ”ฎ (for when Expect feels a little mysterious)

There are a couple of things about Expect that may be non-intuitive. This section attempts to address some of these things with a couple of suggestions.

A common expect problem is how to recognize shell prompts. Since these are customized differently by differently people and different shells, portably automating rlogin can be difficult without knowing the prompt. A reasonable convention is to have users store a regular expression describing their prompt (in particular, the end of it) in the environment variable EXPECT_PROMPT. Code like the following can be used. If EXPECT_PROMPT doesnโ€™t exist, the code still has a good chance of functioning correctly.

set prompt "(%|#|\\$) $"          ;# default prompt
catch {set prompt $env(EXPECT_PROMPT)}

expect -re $prompt

I encourage you to write expect patterns that include the end of whatever you expect to see. This avoids the possibility of answering a question before seeing the entire thing. In addition, while you may well be able to answer questions before seeing them entirely, if you answer early, your answer may appear echoed back in the middle of the question. In other words, the resulting dialogue will be correct but look scrambled. the way that scrambled dialogue is giving chaos ๐Ÿ˜ญ

Most prompts include a space character at the end. For example, the prompt from ftp is 'f', 't', 'p', '>' and <blank>. To match this prompt, you must account for each of these characters. It is a common mistake not to include the blank. Put the blank in explicitly. literally, donโ€™t forget the blank. itโ€™s the main character of the prompt. โœจ

If you use a pattern of the form X*, the * will match all the output received from the end of X to the last thing received. This sounds intuitive but can be somewhat confusing because the phrase โ€œlast thing receivedโ€ can vary depending upon the speed of the computer and the processing of I/O both by the kernel and the device driver.

In particular, humans tend to see program output arriving in huge chunks (atomically) when in reality most programs produce output one line at a time. Assuming this is the case, the * in the pattern of the previous paragraph may only match the end of the current line even though there seems to be more, because at the time of the match that was all the output that had been received.

expect has no way of knowing that further output is coming unless your pattern specifically accounts for it. bestie, it is not psychic. ๐Ÿงš

Even depending on line-oriented buffering is unwise. Not only do programs rarely make promises about the type of buffering they do, but system indigestion can break output lines up so that lines break at seemingly random places. Thus, if you can express the last few characters of a prompt when writing patterns, it is wise to do so.

If you are waiting for a pattern in the last output of a program and the program emits something else instead, you will not be able to detect that with the timeout keyword. The reason is that expect will not timeout - instead it will get an eof indication. Use that instead. Even better, use both. That way if that line is ever moved around, you wonโ€™t have to edit the line itself. okay thatโ€™s highkey clever. ๐Ÿ’…

Newlines are usually converted to carriage return, linefeed sequences when output by the terminal driver. Thus, if you want a pattern that explicitly matches the two lines, from, say, printf("foo\nbar"), you should use the pattern "foo\r\nbar".

A similar translation occurs when reading from the user, via expect_user. In this case, when you press return, it will be translated to a newline. If Expect then passes that to a program which sets its terminal to raw mode (like telnet), there is going to be a problem, as the program expects a true return. (Some programs are actually forgiving in that they will automatically translate newlines to returns, but most donโ€™t.) Unfortunately, there is no way to find out that a program put its terminal into raw mode. ๐Ÿ˜ญ the not-knowing is so real.

Rather than manually replacing newlines with returns, the solution is to use the command "stty raw", which will stop the translation. Note, however, that this means that you will no longer get the cooked line-editing features. so raw mode is giving โ€œno take-backsโ€ energy.

interact implicitly sets your terminal to raw mode so this problem will not arise then.

It is often useful to store passwords (or other private information) in Expect scripts. This is not recommended since anything that is stored on a computer is susceptible to being accessed by anyone. Thus, interactively prompting for passwords from a script is a smarter idea than embedding them literally. Nonetheless, sometimes such embedding is the only possibility. not me telling you to prompt interactively for the secret tea, but also yes. ๐Ÿ“

Unfortunately, the UNIX file system has no direct way of creating scripts which are executable but unreadable. Systems which support setgid shell scripts may indirectly simulate this as follows:

Create the Expect script (that contains the secret data) as usual. Make its permissions be 750 (-rwxr-x---) and owned by a trusted group, i.e., a group which is allowed to read it. If necessary, create a new group for this purpose. Next, create a /bin/sh script with permissions 2751 (-rwxr-s--x) owned by the same group as before.

The result is a script which may be executed (and read) by anyone. When invoked, it runs the Expect script.


๐ŸŽ€ SEE ALSO ๐ŸŽ€ (the reading list, bestie)

  • Tcl(3), libexpect(3)
  • "Exploring Expect: A Tcl-Based Toolkit for Automating Interactive Programs" by Don Libes, pp. 602, ISBN 1-56592-090-2, Oโ€™Reilly and Associates, 1995.
  • "expect: Curing Those Uncontrollable Fits of Interactivity" by Don Libes, Proceedings of the Summer 1990 USENIX Conference, Anaheim, California, June 11โ€“15, 1990.
  • "Using expect to Automate System Administration Tasks" by Don Libes, Proceedings of the 1990 USENIX Large Installation Systems Administration Conference, Colorado Springs, Colorado, October 17โ€“19, 1990.
  • "Tcl: An Embeddable Command Language" by John Ousterhout, Proceedings of the Winter 1990 USENIX Conference, Washington, D.C., January 22โ€“26, 1990.
  • "expect: Scripts for Controlling Interactive Programs" by Don Libes, Computing Systems, Vol. 4, No. 2, University of California Press Journals, November 1991.
  • "Regression Testing and Conformance Testing Interactive Programs", by Don Libes, Proceedings of the Summer 1992 USENIX Conference, pp. 135โ€“144, San Antonio, TX, June 12โ€“15, 1992.
  • "Kibitz - Connecting Multiple Interactive Programs Together", by Don Libes, Software - Practice & Experience, John Wiley & Sons, West Sussex, England, Vol. 23, No. 5, May, 1993.
  • "A Debugger for Tcl Applications", by Don Libes, Proceedings of the 1993 Tcl/Tk Workshop, Berkeley, CA, June 10โ€“11, 1993.

๐Ÿ‘‘ AUTHOR ๐Ÿ‘‘

Don Libes, National Institute of Standards and Technology. king behavior, fr fr. ๐Ÿ‘‘


๐ŸŒธ ACKNOWLEDGMENTS ๐ŸŒธ

Thanks to John Ousterhout for Tcl, and Scott Paisley for inspiration. Thanks to Rob Savoye for Expectโ€™s autoconfiguration code.

The HISTORY file documents much of the evolution of expect. It makes interesting reading and might give you further insight to this software. Thanks to the people mentioned in it who sent me bug fixes and gave other assistance.

Design and implementation of Expect was paid for in part by the U.S. government and is therefore in the public domain. However the author and NIST would like credit if this program and documentation or portions of them are used.


and thatโ€™s the whole thing, bestie! this iconic page is stamped 29 December 1994 and itโ€™s the manual for EXPECT(1). go forth and automate, you absolute legend. ๐Ÿ’–โœจ

๐Ÿ“ฆ where this page comes from