Background Actions

When a background pattern matches, the associated action is evaluated. Evaluation of the action follows the same rules as for a regular expect command. However, inside the action, background patterns for the same spawn id are blocked from further matching. This prevents input that arrived later (i.e., in the middle of an action) from being processed while input associated with the pending action is still being processed.

Any command may be used in the action of a background pattern including another expect or expect_background. expect_background commands allow background patterns from a different spawn id to begin matching immediately—even before the current action finishes. expect commands are executed as usual (i.e., immediately), even if they are for the same spawn id as the one associated with the currently executing background action.

It is not possible to wait using both expect and expect_background for output from the same spawn id at precisely the same time. The behavior in such a situation is undefined.

Get Exploring Expect now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.