I seem to be observing conflicting evidence regarding how the tty and the shell share responsibility for displaying user input.
In the following interactive session, user input is no longer echoed to the terminal, suggesting that the tty performs the echo.
$ stty -echo
# I typed ls (not displayed) and we see the output of ls only:
Bash.log File1 TTY.log
On the other hand, if we use expect to log exactly what is being emitted by bash vs by the tty, we can see that bash seems to be sending the user's input ls to the tty.
tty-echo-investigate.exp
#!/usr/bin/env expect
spawn bash
log_user 0
set log_tty_fd [open "TTY.log" "w"]
set log_bash_fd [open "Bash.log" "w"]
expect {
-i $spawn_id "?" {
send_user -- [set expect_out(0,string)]
puts -nonewline $log_bash_fd [set expect_out(0,string)]
exp_continue
}
-i $tty_spawn_id "?" {
send -- [set expect_out(0,string)]
puts -nonewline $log_tty_fd [set expect_out(0,string)]
exp_continue
}
}
Bash.log
[?2004h~/Play4/X ls
[?2004l
Bash.log File1 TTY.log
[?2004h~/Play4/X exit
[?2004l
exit
TTY.log
ls
exit
When user input is echoed by the tty vs bash, and how do they coordinate so we (usually) see only one copy of user input if they both sometimes do it?
stty -echoin your Expect script.stty -echoin my expect script, a very interesting phenomenon happens. As I am typing, there is no echo, but when I press enter, the text that would have appeared if echo were on suddenly appears, along with the output. asciinema.org/a/NlWwUOhet5vNYwRY2OZ9un35Sstty -echoinside abash -ioutside of expect, I still never see the characters I typed which is further evidence for bash not echoing in this scenario.stty -echo, inside theexpectscript I actually do seelsin my terminal twice -- once from the tty's echo and once from bash's echo. This still leaves open the question of how bash and the tty normally coordinate to not double-echo the user's input. How does bash know it's not supposed to echo user input in the normal case and how does that break down in the expect scenario?