SEXPECT(1)
๐ SEXPECT(1) โ Expect for Shells ๐
okay so bestie, <pink>sexpect is literally Expect for Shells โ yes, that is the whole NAME vibe. Itโs the tool you reach for when you want Expect-style automation but youโre living that shell-script life (sh, bash, ksh, zsh, ...). ๐โจ
๐ธ SYNOPSIS ๐ธ
sexpect [OPTION] SUB-COMMAND [OPTION]
๐ฆ DESCRIPTION ๐ฆ
sexpect is another implementation of Expect, and itโs specifically designed for Shell scripts (sh, bash, ksh, zsh, ...). ๐
sexpect is designed in the client/server model. sexpect spawn starts the server (running as a daemon) and runs the specified PROGRAM in background. Other sexpect sub-commands (send, expect, wait, ...) communicate to the server as client commands. ๐ซถ
๐ GLOBAL OPTIONS ๐
-debug|-dโ Debug mode. The server (sexpect spawn) will run in foreground and more information will be displayed. ๐-help|--help|-hโ Show briefsexpecthelp. ๐-sock SOCKFILE|-s SOCKFILEโ The socket file used for client/server communication. This option is required for most sub-commands. ๐
You can save the SOCKFILE in the SEXPECT_SOCKFILE environment variable so you donโt need to specify the -sock option for each command. For example:
export SEXPECT_SOCKFILE=~/.sexpect-bash.sock
sexpect spawn bash --norc
... ...
The socket file will be automatically created if it does not exist. โจ
-version|--versionโ Showsexpectversion. ๐ท๏ธ
๐ SUB-COMMANDS ๐
Sub-commands may have aliases. For example, sexpect spawn can also be written as sexpect sp or sexpect fork. For each sub-command, the supported aliases are listed in parentheses. iykyk ๐ซ
๐ฃ spawn (sp, fork)
sexpect spawn [OPTION] PROGRAM [ARGS]
The spawn sub-command will first make itself run as a background server. Then the server will start the specified PROGRAM on a new allocated pty. ๐ฅ๏ธโจ
The spawn sub-command supports the following options:
-appendโ See option-logfile. ๐-autowait|-nowaitโ Turn on theautowaitflag which by default is off. See sub-commandsetfor more information. โณ-close-on-exit|-cloexitโ Close the pty after the spawned process has exited even if the spawned processโs child processes are still opening the pty. (Example:ssh -f) ๐ช-nonblock|-nbโ Turn on thenonblockflag which by default is off. See sub-commandsetfor more information. ๐ฆ-idle-close N|-idle Nโ The background server process will close the PTY and exit if there are no client requests in the lastNseconds. Closing the PTY would usually cause the spawned process to receiveSIGHUPand be killed. ๐-logfile FILE|-logf FILE|-log FILEโ All output from the spawned process will be copied to the specifiedFILE. By default theFILEwill be overwritten. Use-appendif you want to append to it. ๐-nohupโ Make the spawned process ignoreSIGHUP. (Example:ssh -f) ๐ โโ๏ธ-term TERM|-T TERMโ Set the environment variableTERMfor the spawned process. This is useful when theTERMvariable is not set (for example for jobs started bycron/crondoratd). ๐ก๏ธ-timeout N|-t Nโ Set the default timeout forsexpect expect. A negative value means no timeout. The default value is-1. โฐ-ttl Nโ The background server process will close the PTY and exitNseconds after the process is spawned. Usually this would cause the process to receiveSIGHUPand be killed. โ-zombie-idle N|-z Nโ When the spawned process closes the pty and exits, if there are no client requests in the lastNseconds the server will automatically wait the spawned process and then exit. This helps avoid too many zombie processes running around. The default value is86400(24 hours). Use-1to disable this. ๐ง
Exit status: 0 will be returned if the background server has successfully started. This does not necessarily mean the PROGRAM has been successfully spawned (e.g. if the specified PROGRAM cannot be found). Non-zero will be returned if the server fails to start for some reason. โ
โ
๐ expect (exp, ex, x)
sexpect expect [OPTION] [ <[-exact] | -glob | -re> PATTERN] [-eof]
If the PATTERN is specified, itโll wait until the PATTERN matches the output of the spawned process. If -eof is specified, itโll wait until EOF (end-of-file) is seen. If neither PATTERN nor -eof is specified, then it defaults to:
expect -timeout 0 -re '.*'
The expect sub-command supports the following options:
-anchor-newline|-anchorโ Used with-re PATTERN. Compile thePATTERNfor newline-sensitive matching by passing theREG_NEWLINEflag toregcomp(3). By default, newline is a completely ordinary character with no special meaning. With this flag, bracket expressions[^...]and.never match newline, a^anchor matches the null string after any newline in the string in addition to its normal function, and the$anchor matches the null string before any newline in the string in addition to its normal function. ๐งต-cstring|-cstr|-cโ C style backslash escapes would be recognized and replaced inPATTERN. See sub-commandsendfor the list of supported backslash escapes. ๐ช-eofโ Wait until EOF is seen from the spawned process. ๐
Note that receiving EOF from the process does not necessarily mean the process has exited. ๐ญ
-exact PATTERN|-ex PATTERNโ Match thePATTERNas an โexactโ string. ๐ฏ-glob PATTERN|-gl PATTERNโ Match thePATTERNas a glob style pattern. For convenience, the glob patterns also support^and$which match the beginning and end of data currently in the internal matching buffer. ๐-lookback N|-lb Nโ Show the most recent lastNlines of output so youโd know where you were last time. ๐ฐ๏ธ-nocase|-icase|-iโ Ignore case when matchingPATTERN. Used with-exact,-globor-re. ๐-re PATTERNโ Match thePATTERNas an extended regular expression (ERE). ๐ง-timeout N|-t Nโ Override the defaultexpecttimeout (seespawn -timeout). โฑ๏ธ
Exit status: 0 will be returned if the match succeeds before timeout or EOF. ๐
If the command fails, the chkerr sub-command can be used to check if the failure is caused by EOF or TIMEOUT. For example (in Bash):
sexpect expect -re foobar
ret=$?
if [[ $ret == 0 ]]; then
# Cool we got the expected output
elif sexpect chkerr -errno $ret -is eof; then
# EOF from the spawned process (most probably dead)
elif sexpect chkerr -errno $ret -is timeout; then
# Timed out waiting for the expected output
else
# Other errors
fi
๐ค send (s)
sexpect send [OPTION] [ [--] STRING | -file FILE | -env NAME]
The send sub-command sends data to the spawned process. ๐จ
Note that the data to be sent must be less than1024bytes. To send more data, use multiplesexpect sendcommands. ๐
The send sub-command supports the following options:
-cstring|-cstr|-cโ C language style backslash escapes would be recognized and replaced inSTRINGbefore sending to the spawned process. ๐ช
The following standard C language escapes are supported:
\\ \a \b \f \n \r \t \v
\xHH \xH
\ooo \oo \o
Other supported escapes:
\e \E : ESC, the escape char.
\cX : CTRL-X, e.g. \cc will be converted to the CTRL-C char.
-enter|-crโ Append ENTER (\r) to the specifiedSTRINGbefore sending to the spawned process. โฉ๏ธ-fd FDโ Read data from file descriptorFDand send to the spawned process. Option-limitmust be specified with-fd. Use-stripto remove trailing white space chars. ๐ฅ-file FILE|-f FILEโ Send the content of theFILEto the spawned process. IfFILEis-then stdin will be used. Option-limitmust be specified ifFILEis not a regular file. Use-stripto remove trailing white space chars. ๐-env NAME|-var NAMEโ Send the value of environment variableNAMEto the spawned process. ๐ฑ-limit LIMITโ Used with-fileor-fd. Read at mostLIMITchars from the file.LIMITshould be in range[1, 1024]. ๐ข-stripโ See option-file. ๐งผ
๐ฎ interact (i)
sexpect interact [OPTION]
The interact sub-command is used to attach to the spawned process and manually interact with it. To detach from the process, press CTRL+] (CTRL-]). ๐น๏ธ
interact would fail if itโs not running on a tty/pty. ๐ฌ
If the spawned process exits when youโre interacting with it then interact will exit with the same exit code of the spawned process and you donโt need to call the wait sub-command any more. And the background server will also exit. ๐
The interact sub-command supports the following options:
-lookback N|-lb Nโ Show the most recent lastNlines of output after attaching to the process so youโd know where you were last time. ๐ฐ๏ธ-nodetach|-nodetโ DisableCTRL-]. This may be useful in scripts. ๐-re PATTERNโ Automatically detach from the spawned process when the output matches the RE pattern. After a successful match, you can useexpect_outto get substring matches. ๐ฏ<-subst | -sub> PATTERN::REPLACEโ Dynamically monitor output from the spawned process and if the output matches thePATTERNthen change the matching part toREPLACE. ๐
REPLACE can include (0), (1), (2), ..., (9) for RE match groups. (0) is for the whole match and (1) .. (9) for corresponding subgroups. For example, -subst "(foo)(bar)::(0)-(1)-(2)" would change "foobar" to "foobar-foo-bar". โจ
The PATTERN cannot include "::". ๐ซ
At most 10 -subst options can be specified. ๐
-cstring|-cstr|-cโ Used with-re PATTERNor-subst PATTERN::REPLACE. C style backslash escapes would be recognized and replaced inPATTERNorREPLACE. See sub-commandsendfor the list of supported backslash escapes. ๐ช-nocase|-icase|-iโ Used with-re PATTERNor-subst PATTERN::REPLACE. Ignore case when matchingPATTERN. ๐-anchor-newline|-anchorโ Used with-re PATTERNor-subst PATTERN::REPLACE. See sub-commandexpectfor more details.. ๐งต
โณ wait (w)
sexpect wait
The wait sub-command waits for the spawned process to complete and return the spawned process' exit code. ๐ซถ
expect_out (expout, out) ๐
okay bestie, so picture this: your expect sub-command just successfully matched the PATTERN you gave it, and now you want the substring matches? Thatโs literally what expect_out is for. โจ
sexpect expect_out [< -index | -i> INDEX]
The server side saves up to 9 (1-9) RE substring matches. 0 refers to the string that matched the whole PATTERN. If you donโt pass INDEX, it defaults to 0. No cap. ๐
Example: if this command
sexpect expect -re 'a(bc)d(ef)g'
succeeds (exits 0), then these commands
sexpect expect_out -index 0
sexpect expect_out -index 1
sexpect expect_out -index 2
would output abcdefg, bc, and ef, respectively. Slay. ๐
chkerr (chk, ck) ๐ฉบ
If the previous expect sub-command fails, chkerr checks whether the failure is caused by EOF or TIMEOUT. See the expect sub-command for an example.
sexpect chkerr -errno NUM -is REASON
Options:
-errno NUM,-err NUMโNUMis the exit code of the previous failedexpectsub-command.-is REASONโREASONcan beeof,timeout.
Exit status: 0 will be returned if the specified error NUM is caused by the REASON. 1 will be returned if the specified error NUM is NOT caused by the REASON. Itโs giving error vibe check. ๐
close (c) ๐ช
sexpect close
The close sub-command closes the spawned processโs pty by force. This would usually cause the process to receive SIGHUP and be killed. dramatic but effective. ๐
kill (k) โก
sexpect kill [-SIGNAME | -SIGNUM]
The kill sub-command sends the specified signal to the spawned process. The default signal is SIGTERM.
Options:
-SIGNAMEโ Specify the signal with name. Only the following signal names are supported:
SIGCONT SIGHUP SIGINT SIGKILL SIGQUIT
SIGSTOP SIGTERM SIGUSR1 SIGUSR2
SIGNAME is case insensitive and the prefix SIG is optional. iykyk ๐ซถ
-SIGNUMโ Specify the signal with number.
set โ๏ธ
sexpect set [OPTION]
The set sub-command can be used to dynamically change server sideโs parameters after spawn. Lowkey powerful. ๐ฅ
Options:
-autowait FLAG,-nowait FLAGโFLAGcan be0,1,on,off.
By default, after the spawned process exits, the server side will wait for the client to call wait to get the exit status of the process and then the server will exit. When autowait is turned on, after the spawned process exits itโll be automatically waited and then the server will exit.
-nonblock FLAG,-nb FLAGโFLAGcan be0,1,on,off.
By default, the spawned process will be blocked if it outputs too much and the client (either expect, interact or wait) does not read the output in time. When nonblock is turned on, the output from the process will not be blocked so the process can continue running.
-idle-close N,-idle Nโ Set theIDLEvalue. See thespawnsub-command for details.-timeout N,-t Nโ See thespawnsub-command for details.-ttl Nโ See thespawnsub-command for details.
get ๐
sexpect get [OPTION]
Retrieve server side information.
Options:
-all,-aโ Get all available information from server side.-autowait,-nowaitโ Get theautowaitflag.-nonblock,-nbโ Get thenonblockflag.-idle-close,-idleโ Get theIDLEvalue. Seespawnfor details.-pidโ Get the spawned processโs PID.-ppidโ Get the spawned processโs PPID.-tty,-pty,-ptsโ Get the spawned processโs tty.-timeout,-tโ Get the current default timeout value.-ttlโ Get theTTLvalue. Seespawnfor details.-expect-buf N,-expbuf Nโ Dump the most recentN(at most4096) chars from the internal expect buffer.
๐ฑ ENVIRONMENT VARIABLES ๐ฑ
SEXPECT_SOCKFILEโ See GLOBAL OPTIONS for details. your env var bestie is important โจ
๐ RESOURCES ๐
Project home: https://github.com/clarkwang/sexpect/
๐ SEE ALSO ๐
expect(1), pty(7), pts(4), glob(3), fnmatch(3)
๐ AUTHOR ๐
Written by Clark Wang <dearvoid AT gmail.com>.
๐ REPORTING BUGS ๐
Report bugs to Clark Wang <dearvoid AT gmail.com> or open an issue at https://github.com/clarkwang/sexpect/issues/
This page is serving sexpect 2.3.14 realness, dated 2022-09-16 โ periodt, bestie. ๐โจ
๐ฆ where this page comes from
- alpine 3.24: sexpect-doc 2.3.15-r0 ยท GPL-3.0-only
- ๐ girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms