Automating Interactive Processes with Expect on macOS

Scripting processes that call interactive CLIs can be time consuming and hard to get dialed in. Learn how to simplify such processes with Expect, a utility that ships with macOS, in this post.

Automating Interactive Processes with Expect on macOS

Expect is a Unix based utility that is a common means of automating processes that require user input. Scripts written with expect work by “expecting” a specific prompt or response from the interactive process being called, and passing the appropriate response when prompted. This could be user credentials, specific keystrokes or anything a user might be prompted for in an interactive CLI.

These scripts can be written manually, but today, we’ll look at a simple, automated way of creating the expect scripts themselves. To do so, we’ll need to install autoexpect, which we can achieve on macOS by running the following:

brew install expect 

Then, we’ll need to wrap our targeted process in a bash script that we can pass as an argument to autoexpect, like so:

autoexpect /path/to/our/ 

This command tells expect to call the process you’d like to automate, and then walks you through the actual interactive prompts associated with the process. It then produces an expect script called script.exp that – when called – will call your targeted process once more, but this time it will enter the same input you passed manually, but in an automated fashion.

You can then execute the original process defined in your file by calling ./script.exp.

NOTE: the filepath to our is hardcoded into the script.exp, so it’s important to put it where you’d like it to live in the filesystem before passing the filepath as an argument to autoexpect, as shown above.

Automating Homebrew installation

To demonstrate this process, let’s create a pair of scripts that install Homebrew without requiring an interactive user session. First, we’ll create a script called, which will need to contain the following:

/bin/bash -c "$(curl -fsSL"

Then, with autoexpect installed, we can create our autogenerated expect script by running the following:

autoexpect /path/to/

When we do, we’ll be prompted as we normally would when installing Homebrew, which will look like the following:

admin@admins-iMac-Pro ~ % autoexpect /Users/admin/
autoexpect started, file is script.exp
==> Checking for `sudo` access (which may request your password)...
==> This script will install:

Press RETURN/ENTER to continue or any other key to abort:
==> /usr/bin/sudo /usr/sbin/chown -R admin:admin /usr/local/Homebrew
==> Downloading and installing Homebrew...

Once this process is completed, it will produce a script in the local directory called script.exp.

Then, we can test the autogenerated script by running ./script.exp. If all went well, we should see the same output as is shown above, but our new expect script will submit the same input we provided when we created it automagically.


Expect is a powerful, Unix-based utility that allows users to automate interactions with interactive command line interfaces (CLIs). They can be very handy for automating the provisioning of new machines remotely. For example, above we looked at the example of automating the installation of Homebrew, which would normally require that a user manually remoted into a server. However, with our nifty expect script, we can achieve the same result in a fully automated way.