Yeah, subst seems like the best solution but his problem is shell/interpreter quoting.
Let me show both tcl and shell solutions.
Tcl:
If we put all this into a tcl file and source it from within expect, then this is how you get the tcl quoting and escaping of special characters correct. Outside of tcl on the command line you have to additionally account for the shell/interpreter.
% expect
expect1.1> source ./j.tcl
hello
1,goodbye to you
expect1.2> exit
% cat ./j.tcl
set foo hello
system "echo $foo\; set -- \"goodbye to you\"\; echo \$#,\$1"
Here is the shell/cmd line fix:
Original:
% expect -c 'set foo hello;system echo $foo\;set -- "goodbye to you"\;echo \$#,\$1'
(failed, see original error msg)
Fixed:
% expect -c 'set foo hello;system "echo $foo\;set -- \"goodbye to you\"\; echo \$#,\$1"'
hello
1,goodbye to you
(added double quotes around the cmds passed to system call)