I know that hard brackets [, ] require triple backslash escaping when used inside expect "..." strings, so I use expect "blah blah \\\[herp derp\\\]" to accurately convey these characters to the expect I/O checker, but what other characters also require escaping? Pipe? Parentheses? ???
- 3,098
1 Answers
There's 2 things going on here:
Tcl uses
[...]as the "command substitution" syntax (See https://tcl.tk/man/tcl8.6/TclCmd/Tcl.htm rule 7). It's like backticks in a shell script. Within double quotes, command substitutions are performed.The default pattern matching for the
expectcommand is "glob" patterns. Glob patterns are documented here: https://tcl.tk/man/tcl8.6/TclCmd/string.htm#M35 Square brackets are special for glob patterns.
So, first you need to protect the square brackets within double quotes to prevent command substitutions. Then, if you want to treat them as literal characters, you need to escape them again.
Here are a couple of things you can to do reduce the backslashes:
use non-interpolating quotes: in Tcl that is
{braces}not"quotes".choose a different pattern type: if this is a literal string you want to match, use the
-exoption to say you want an "exact" match.
Thus, you want this:
expect -ex {blah blah [herp derp]}
- 27,524