What is TTY?
sobyte.net
sobyte.net
See this tweet [1]
Update: it even didn’t change its example of using original blog’s domain name. [2]
0: https://www.kawabangga.com/posts/4515
1: https://twitter.com/laixintao/status/1558667661420404737
2: > Imagine a scenario where you break into someone’s machine, such as the server where kawabangga.com is located, and you find a way to execute python code inside it, but you can only inject the code into it and execute it without seeing the output, what do you do?
In fact, the image with the “hardware” and “software” diagrams seems to have been copied directly from that link. I did not see any attribution or acknowledgement.
You can see from the original post[0], I add a link, a description and credits to every image that not created by me.
However, I accept all other technical criticism under this threads, I didn't dig deep enough while writing this. I will learn more.
Like for example the author confuses the terminal for the shell. ctrl-w very much is passed right through to bash.
But the author is correct if the user types "cat\n". Now it's the terminal handling ctrl-w. But note that cat doesn't handle ctrl-a.
It's more complex than the author seems to think.
tl;dr: read the other post, not this one.
This is more complicated (or simpler?) than what the article explains. Or I might be really confused. But if you strace bash while typing in stuff, you see clearly that it is reading in characters one by one, including ^W, and handling them itself. I don't see TTY doing much magic here at all:
read(0, "h", 1) = 1
write(2, "h", 1) = 1
read(0, "e", 1) = 1
write(2, "e", 1) = 1
read(0, "l", 1) = 1
write(2, "l", 1) = 1
read(0, "l", 1) = 1
write(2, "l", 1) = 1
read(0, "o", 1) = 1
write(2, "o", 1) = 1
read(0, " ", 1) = 1
write(2, " ", 1) = 1
read(0, "w", 1) = 1
write(2, "w", 1) = 1
read(0, "o", 1) = 1
write(2, "o", 1) = 1
read(0, "r", 1) = 1
write(2, "r", 1) = 1
read(0, "l", 1) = 1
write(2, "l", 1) = 1
read(0, "d", 1) = 1
write(2, "d", 1) = 1
read(0, "\x17", 1) = 1 <-- bash reading in ^W!
write(2, "\x08\x08\x08\x08\x08\x1b\x5b\x4b", 8) = 8 ioctl(0, SNDCTL_TMR_STOP or TCSETSW, {B38400 opost isig icanon echo ...}) = 0
clone(child_stack=NULL, flags=CLONE_CHILD_CLEARTID|CLONE_CHILD_SETTID|SIGCHLD, child_tidptr=0x7fe653abef10) = 5175
wait4(-1, [{WIFEXITED(s) && WEXITSTATUS(s) == 0}], WSTOPPED|WCONTINUED, NULL) = 5175
--- SIGCHLD {si_signo=SIGCHLD, si_code=CLD_EXITED, si_pid=5175, si_uid=1000, si_status=0, si_utime=0, si_stime=0} ---
wait4(-1, 0x7fff156cbad0, WNOHANG|WSTOPPED|WCONTINUED, NULL) = -1 ECHILD (No child processes)
ioctl(0, SNDCTL_TMR_STOP or TCSETSW, {B38400 opost isig -icanon -echo ...}) = 0
https://man7.org/linux/man-pages/man4/tty_ioctl.4.htmlIf you set this to something else (stty werase ^O) you'll see it takes effect immediately.
Also if you disable werase i.e. "stty werase ''" and use the bind bash builtin to set "bind Control-w:' backward-kill-word'" you will get slightly different behaviour. A hyphen will be considered a word ending, unlike the tty.
So does bash read ^w and mimics the tty behaviour regarding the definition of a word?
I don't have linux handy right now and tracing on macos is painful otherwise I'd dive deeper.
> The answer is Ctrl+W. Because Ctrl+W is provided by something called TTY, and the other three are provided by the shell.
My only criticism is a pet peeve of mine: screenshots of terminals. I know it's more work to copy/paste and make sure formatting is correct, and you lose the ANSI colors. But it just feels so wasteful and inaccessible (<img alt="tty">).
Some terminal emulators also support dumping the buffer as HTML.
Indeed, I mentioned aha in another comment.
> Some terminal emulators also support dumping the buffer as HTML.
That's a nifty feature that I haven't run into yet. Seems like it could be quite useful.
It is, however, a needlessly complex abstraction as of now. The lack of a real terminal device means that the OS maintains such pseudo devices for the sole purpose of being able to spawn multiple virtual terminals.
Contrast this with Windows; until recently, command-line applications there were pretty tightly coupled to the console host that they ran within [1]; this made remoting of command-line applications difficult and means you had to go through extraordinary hoops just to do things like replace the system's console UI with something like ConEmu or run a terminal within something like Visual Studio Code. More recent versions of Windows try to improve this situation, by adding support for Unix-style pseudoterminals [2] (what they call ConPTY) to the console subsystem. And since ConPTY terminals are based around the same standards as TTY terminals on *nix, things like OpenSSH remoting can now "just work", even for existing command line apps on Windows.
[1] See here for some of the gory details of how this works and how it differs from the conventional *nix approach: https://devblogs.microsoft.com/commandline/windows-command-l...
[2] Blog post describing the PTY infrastructure on Windows: https://devblogs.microsoft.com/commandline/windows-command-l...
Its brokenness is debatable, see for example https://lwn.net/Articles/343828/
Another example: The author touches on line discipline, but then makes the mistake of saying "The command is actually stored in the TTY after it is typed and before the Enter key is pressed" This isn't true in bash, or on any modern shells that support line editing features like tab completion.
If you're going to use this for an interview question, I suggest using cat(1) instead -- it will behave as the author suggests.
Ctrl-A vs Ctrl-E vs Ctrl-W vs up arrow. The correct answer is the up arrow and any interviewer who disagrees will walk out of the room with a broken nose. Bash, readline, etc, operate in raw mode so the tty driver isn't handling anything. Three are control characters - a single character. Meanwhile, a vt100 can generate 2 different sequences for the up arrow -- ESC [ A or ESC O A -- depending on cursor key mode. Either way, that's 3 characters instead 1 character. (There's also a third sequence -- ESC A -- in vt52 mode but we'll ignore that).
Did they mean Ctrl+B to move a letter backward?
Ctrl+F moves a letter forward.
https://github.com/cosmos72/twin
A couple screenshots are on the old SourceForge site.