Consider the following expect script:
spawn bash
expect " \$"
send "tmux\r"
expect " \$"
send "echo hello\r"
send "\x02"
send "d"
interact
I expect this script to create a tmux session, execute "echo hello", and then detach.
The first two parts work great, but instead of detaching, I just get the literal characters ^Bd on my terminal.
Is it possible for expect to send the Control-B, d sequence to tmux exactly like what happens if I type it on the keyboard?
I fiddled with this a bit, and empirical evidence suggests that the problem is with
tmuxrather thanexpect, butexpectcan work around it by adding a small delay.In particular, it seems that if
expectsends the sequence too quickly after the previous command terminated,tmuxdoes not interpret the sequence and echos it instead. Curiously,screendoes not have this problem.