Backslashes
Tcl makes various substitutions when you have backslashes, dollar signs, and brackets in command arguments. You should be familiar with these from Chapter 2 (p. 23). In this section, I am going to focus on backslashes.
Backslash translations are done by Tcl only when processing command arguments. For example, \n is translated to a linefeed, \[ is translated to a "[“, and \\ is translated to a "\“. Sequences that have no special translation are replaced by the character without the backslash. For example, \z is translated to a "z“.
While pattern matching, Expect uses these translated values. For example:
expect "\n" ;# matches \n (linefeed character)
expect "\r" ;# matches \r (return character)
expect "\z" ;# matches z (literal z)
expect "\{" ;# matches { (literal left brace)
If any backslashes remain after Tcl’s translation, the pattern matcher (i.e., pattern matching algorithm) then uses these remaining backslashes to force the following character into its literal equivalent. For example, the string "\\*" is translated by Tcl to "\*“. The pattern matcher then interprets the "\*" as a request to match a literal "*“.
expect "*" ;# matches * and ? and X and abc expect "\\*" ;# matches * but not ? or X or abc
Similarly, backslashes prevent a ? from acting like a wildcard.
expect "?" ;# matches * and ? and X but not abc expect "\\?" ;# matches ? but not * or X or abc
So that you can see the consistency here, I have written out some more examples. Do not try to memorize these. ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access