In expect it is possible to spawn a process like,

spawn /usr/bin/sftp "$uri" /home/user/dest

Writing it like this makes things really simple, it will be a one liner. However, I am trying to replicate this for upload. This is a bit more complicated,

spawn /usr/bin/sftp "$uri" <<< $'put "$source"'

Most likely the issue is that this is bash code and not a single command. Is there a way to spawn something like this in expect, or will I have to do it the long way? I also tried with braces, to no success.

BR Patrik

  • How are you running this? in a bash script or an expect script>
    – Inian
    Commented Sep 4, 2019 at 8:40
  • @Inian Running pure expect, but it will always be run from a bash shell.
    – patrik
    Commented Sep 4, 2019 at 8:50
  • I've always found perl's Expect.pm module (and the various protocol-specific modules like Net::SSH, Net::Telnet, and Net::SFTP, etc) to be a lot more useful and easier to work with than expect itself. Amongst other benefits, there's no need for messing around with shell quoting or escaping, just use perl vars where they're needed. Python has similar capabilities with, e.g., the pexpect library.
    – cas
    Commented Sep 5, 2019 at 0:50
  • Also, I think it's a waste of time learning a single-purpose language like expect, especially when you'll use it only rarely. IMO that time is better spent learning or improving your skill with a general purpose language like perl or python, which you'll use far more often for many and varied tasks.
    – cas
    Commented Sep 5, 2019 at 0:53
  • @cas not sure if "improving in perl" is completely accurate. I managed to get fairly fluent in perl in about 3 weeks (though this knowledge may not extend to big perl apps, if there ever is a point for these). Anyway, expect is very useful for short scripts where you just want to connect to a remote system, do just two things and terminate. I cannot say my expect skills are the result of more than about 8 hours of learning and this is sufficient for almost anything I use expect for.
    – patrik
    Commented Sep 5, 2019 at 7:24

1 Answer 1


Well, this is embarrasing. As soon as I formulated the questions the solution began to clarify. Of course it is possible to spawn a bash shell and run whatever commands I want there,

spawn /bin/bash
send "/usr/bin/sftp \"$uri\" <<< $\'put \"$source\"\'\r"

Anyway, for all it's worth there might be more people running into this problem so I will post a solution to this instead of removing the post.

  • Note, you can also use {...} to quote a string and it will not need any special backslash escapes (except for nested {}). See the section [6] Braces in tcl
    – meuh
    Commented Sep 4, 2019 at 9:36

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .