Expect is a powerful tool for automating interactive applications.

It runs script files written in the esoteric “Tool Command Language”, TCL.

The docs are here:

To invoke expect you simply point it at your script file:

expect -f <script> [any other args]

Handle a known sequence of prompts

spawn annoying_program_with_unskippable_prompt
expect "Is this OK?"
send "yes"
expect "Are you sure?"
send "YES!"

Handle an unknown sequence of prompts

expect {
    "Say yes:" {
        send "yes"
    "Say no:" {
        send "no"

exp_continue resumes execution, so this is effectively an infinite loop.

Exit when the spawned command ends

expect eof

Parameterise the script with shell arguments

argv and argc are special variables that hold the values and number of arguments:

expect <script> a -b -c -d
argc = ["a", "-b", "-c", "-d"]
argv = 4

If you want to use these in a spawn command, they can be extracted like so:

spawn [lindex $argv 0] {*}[lrange $argv 1 end]

This would execute:

spawn a -b -c -d