I have an expect script that spawns a connection to hunspell to check the spelling of a word. Unfortunately, if the word is in the dictionary, hunspell simply sends back a *. How can I tell expect to interpret it as a character instead of as "anything?"


There's two layers: the pattern matcher, and TCL. The trick is to backslash a backslash so the subsequent star isn't special after it works its way down through the layers involved.

#!/usr/bin/expect -f

#spawn echo "cat"
spawn echo "*"
expect {
    "cat" { set gots "dog" }
    "\\*" { set gots "star" }

puts $gots
  • Many thanks. As it turns out, the script was not waiting correctly for a complete reply before moving on to the next item in the loop, then matching the wrong reply from the spawned process. When I saw the mismatches (successes that should have been failures), I mistakenly thought \* wasn't escaping as I wanted it to. As it turned out, I needed to add another expect statement to wait for some carriage returns from the spawned process before the loop was allowed to continue. Again, thanks for the prompt reply! – m00tpoint Oct 27 '15 at 15:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.