tl;dr Add -ex flag.
The thing here is that expect makes it easier to pass complex arguments to some commands (expect/interact):
The commands that accepted arguments braced into a single list (the expect variants and interact) use a heuristic to decide if the list is actually one argument or many. The heuristic can fail only in the case when the list actually does represent a single argument which has multiple embedded \n's with non-whitespace characters between them. This seems sufficiently improbable, however the argument "-nobrace" can be used to force a single argument to be handled as a single argument. This could conceivably be used with machine-generated Expect code. Similarly, -brace forces a single argument to be handle as multiple patterns/actions.
(from description of expect command) If the arguments to the entire
expect statement require more than one line, all the arguments may be "braced" into one so as to avoid terminating each line with a backslash. In this one case, the usual Tcl substitutions will occur despite the braces.
Let me give an example here:
#!/usr/bin/env bash
set -eu
f() {
echo line 1
sleep 1
echo line 2
}
export -f f
echo '
spawn bash -c f
expect {
{line 1} {puts 1}
{line 2} {puts 2}
}
expect \
{line 1} {puts 1} \
{line 2} {puts 2}
' | expect
Output:
spawn bash -c f
line 1
1
line 2
2
In the repository I've found, the heuristic in the latest commit (May 2014) is as follows: if one argument is passed and there is a newline before first non-whitespace, the argument is considered to be multiple arguments braced into one.