free web page hit counter
🛡️
Copyright Notice: This video is officially sourced and embedded from YouTube. For all copyright inquiries, reports, or removals, please contact YouTube's legal team here.
The Cef Experience

The Cef Experience

1,450 subscribers

⏱ 👁 7,413 views

Terminals, TTY, PTY, and ANSI Escape Codes

Video Overview & Insights

## Chapters

“

Good video but please try using a better mic man

— @rudrasatwik

* **0:00** - Introduction to Terminals, PTY, and TTY

* **0:08** - Running Commands and the Role of the Shell (e.g., Zsh, Bash)

“

This was awesome, thank you. You should add a link to this youtube video in your article. I landed on it first and only much later found this video.

— @samlaf92

* **1:50** - What is TTY (Teletypewriter)?

* **2:09** - Virtual Consoles and Terminal Emulators

“

Thanks that very helpful

— @Anonymous51701

* **2:41** - Pseudo-Terminals (PTY) and Terminal Simulation

* **2:50** - ANSI Escape Codes for Formatting (Color, Underline, Bold)

“

Great video, literally I was working with python's prompt_tookit and was wondering what the hell was raw and "cooked" mode. Would love more info on running a terminal emulator with a remote shell in an embedded device, something I'm looking at right now

— @ridgeAndcompany-nm7qt

* **3:57** - Interpretation of ANSI Codes by Terminal Emulators

* **4:46** - Parsing ANSI Codes (Example with `pyte` and HTML)

“

Well explained

— @sajanpatel7140

* **6:29** - Processes, Controlling Terminals, and Signals (Ctrl+C, SIGHUP)

* **7:47** - How PTY Works and Why it's Needed

“

nice video; great explanation. subbed!

— @desilvade

* **8:32** - Line Discipline: Cooked Mode (Canonical) vs. Raw Mode

* **9:40** - Line Discipline: Echoing

“

Nice video. Love your enthusiasm while explaining these topics.

— @sudoggo

* **9:53** - Changing Terminal Options with `stty` (Disabling Canonical Mode and Echoing)

* **10:41** - Signal Management and Flow Control (Ctrl+S, Ctrl+Q)

“

Great explanation got to know something new!

— @gauravdeep01

* **11:49** - Window Size and Resizing Events (SIGWINCH)

* **13:47** - PTY and Remote Connections (SSH and PTY)

* **14:58** - Summary

***

Blog post: https://cefboud.com/posts/terminals-pty-tty-pyte/

## Terminal Fundamentals: Shell, TTY, and PTY

[cite_start]When you open a terminal window, you're running a **terminal emulator**[cite: 22]. [cite_start]The program that reads and executes your commands is the **shell** (e.g., Zsh, Bash, Fish)[cite: 9, 10, 11]. [cite_start]The shell parses the command, finds the executable file, and runs it within a child process[cite: 12, 13, 14]. [cite_start]The output is then displayed on the screen[cite: 15].

* [cite_start]**TTY (Teletypewriter)**: This is the term used to refer to any terminal, whether physical or virtual[cite: 17]. [cite_start]**Virtual consoles** are TTYs built into the Linux kernel that operate without a graphical user interface (GUI)[cite: 19].

* [cite_start]**PTY (Pseudo-Terminal)**: This is an OS functionality that simulates the behavior of a terminal[cite: 21]. [cite_start]Terminal emulators rely on PTYs[cite: 21]. [cite_start]PTYs act as a pipe with two ends, one for the terminal emulator and one for the command, making the command think it's connected to an actual terminal[cite: 57, 58].

***

## ANSI Escape Codes

[cite_start]**ANSI escape codes** are sequences that instruct the terminal how to format the words being displayed[cite: 68, 70, 71]. These codes allow for:

* [cite_start]Coloring text[cite: 72].

* [cite_start]Underlining and bolding text[cite: 73, 74].

* [cite_start]Moving the cursor and clearing the screen[cite: 77, 88].

* [cite_start]Making sounds (e.g., the bell sound using $\text{NC code 7}$)[cite: 80, 81].

[cite_start]The terminal emulator itself is responsible for interpreting these codes[cite: 89, 90, 91].

***

## Terminal Behavior and Line Discipline

Commands run in a terminal are attached to a **controlling terminal**. The PTY/TTY functionality provides essential terminal behavior through a feature called **line discipline**.

Key aspects of terminal behavior offered by TTY:

* **Buffering (Canonical/Cooked Mode)**: Input characters are buffered and only sent to the process when the user hits Enter. Programs like text editors can switch to **raw mode** to receive characters as they are typed.

* **Echoing**: When you type, the TTY module echoes the characters back to the screen so you can see them.

* **Signal Management**: The terminal handles signals like the interrupt signal ($\text{SIGINT}$) sent with $\text{Ctrl}+\text{C}$ to terminate a process. If a parent terminal closes, a hangup signal ($\text{SIGHUP}$) is sent to terminate all linked processes.

* **Flow Control**: The TTY subsystem manages flow control, such as blocking standard output with $\text{Ctrl}+\text{S}$ and resuming with $\text{Ctrl}+\text{Q}$.

* **Window Size/Resize**: The terminal has a size in rows and columns. When the window is resized, a $\text{SIGWINCH}$ signal is dispatched to programs so they can adjust.

PTYs are crucial for modern applications like **SSH**, where a pseudo-terminal is started on the remote server to simulate a local terminal session and handle input/output and resize events over the network.