use expect to spawn command with arguments containing spaces use expect to spawn command with arguments containing spaces bash bash

use expect to spawn command with arguments containing spaces


Single quotes (') have no special meaning to Expect, unlike sh and other compatible shells.
This means that your statment

spawn ssh root@localhost 'cat /tmp/id_rsa.pub'

is parsed into the following words:

  • spawn
  • ssh
  • root@localhost
  • 'cat - not until the other single quote.
  • /tmp/id_rsa.pub'

The usage in sh is to group this to a single argument. In Tcl you could either use double quotes (") or curly brackets ({}). Inside double quotes, Tcl variables will be substituted, while the content inside {} is passed without any substitution1.

tl;dr The Expect/Tcl equivalent of sh's ' are {}.

1 A \ before a newline will still be substitued.


As Johannes said, using ' doesn't work.
I had a similar problem, but I wanted to execute more commands and then still get a login shell. I managed to make it work with ":

expect -c "   set timeout 5;   spawn ssh -XY $user@$host -t \"cat /etc/motd; bash -l\"   expect {      -re \"^Warning.*\" {exp_continue}      -re \"^.*sword: \" {send \"${PASSWORDS[$user]}\r\"; interact}   }"


I guess you don't require eval there. Remove it and check.

Check this expect examples link for detailed examples on "how to use expect?" .

Hope this helps.