Avoiding Echoing

A problem with -echo is that a user may start entering a pattern that echoes but then go on to enter other characters that do not match the remainder of the pattern. The buffered characters plus the new ones will be sent to the spawned process. Assuming that the spawned process echoes them, if a pattern using -echo was causing them to echo already, the user will see characters echoed twice.

For example, in the recording script above, suppose the user enters ~q. The tilde matches and is echoed by Expect. The q does not match, so the tilde and q are sent to the spawned process and are likely echoed by the spawned process. Unfortunately, there is no trivial way to “unecho” a character without getting involved in knowing how to erase characters on the screen as well as what was on the screen in the first place. So the user ends up seeing "~~q“—one tilde too many.

There is no perfect solution for this problem except to avoid it to begin with (perhaps by not using -echo at all). But the next best possibility is to choose patterns that are less likely to match at any point unless the user is definitely entering a pattern.

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.