Confession time: I don’t fully understand how terminals, shells and SSH really work (and my guess is you don’t either). And I don’t mean the cryptography behind SSH. I mean how SSH and the terminal — and the shell for that matter — interact with one another.
I recently realized that even though I’ve been daily remotely logging into Linux systems for all of my adult life (and type in the shell and Vim) I didn’t really grasp how these things actually work.
I mean, it’s one of those things I kind of understand, but not completely. The author of the short linked post found four articles that detail all this stuff quite well, so go on over there and see just how well you really understood it.
I’ve worked with low level TTY APIs in the past and I strongly dislike the TTY foundations. It’s polluted with archaic things like line printers and telnet control sequences that IMHO belong in the shell rather than the kernel. Instead on unix many of these get implemented in the kernel and you use the obscure terminal API to program it. If you get it wrong it can mess with your IO. Also the way job control is managed by the kernel is very awkward to integrate with shells. I’d love to dump this complexity in favor of something much simpler but we can’t drop it now because so many things depend on leaving it as is for compatibility.
Just look at some of these things…
https://linux.die.net/man/3/termios
https://man7.org/linux/man-pages/man4/tty_ioctl.4.html
Luckily I don’t have to program at this level very often, but if ever you do, you’ll get thrown into a world of legacy hacks.
I expect a lot of that was to do with when UNIX was developed. In the 70’s, mainframes and minicomputers were almost entirely accessed via text-only “TTY” descended terminals. Because this was the norm for many years, it seemed fair to assume it would stay the norm for the next 20 years. Sadly, the world soon changed, but not before UNIX became a staple OS in industry and academia, and largely standardised by official standards. Sadly, it’s likely this standardisation which enforced such legacy crap in modern OSes. Just because that’s where it was when the standards were defined.
Want a fun time? Try writing a terminal emulator using a high-level programming language. I did this in my final years of college (so 15ish years ago) for a side project. The best “documentation” I could find was simply to reference other people’s implementations. It certainly did teach me some of the underbelly of what we use every day and don’t think about.