Terminal¶
21 cards — 🟢 5 easy | 🟡 8 medium | 🔴 2 hard
🟢 Easy (5)¶
1. What does a terminal emulator do?
Show answer
It converts user input (keystrokes, mouse events) into bytes sent to programs, and renders output bytes as text, colors, and cursor movement on screen.Remember: "Terminal emulator = keyboard/screen translator." It doesn't run commands — the shell does. The terminal is just the I/O interface.
Example: iTerm2, GNOME Terminal, Windows Terminal, Alacritty are all terminal emulators.
2. What does the shell do?
Show answer
It parses commands, expands globs and variables, sets up redirects and pipes, manages jobs, and launches programs.Remember: "Shell = command interpreter." bash, zsh, fish are shells. They run inside a terminal emulator.
Gotcha: Changing your terminal emulator doesn't change your shell, and vice versa.
3. What does the PATH environment variable control?
Show answer
It specifies the directories the shell searches, in order, to find executable programs when you type a command.Remember: "PATH is searched left to right." First match wins. Prepend to override system commands, append to add new ones.
Example: export PATH="$HOME/bin:$PATH" makes ~/bin commands take priority.
4. How do you suspend a running foreground process and resume it?
Show answer
Ctrl+Z suspends (stops) it. Then use bg to continue it in the background, or fg to bring it back to the foreground. Use jobs to list current jobs.Remember: "Ctrl+Z = SIGTSTP (stop/suspend), fg = resume foreground, bg = resume background."
Example: Run a long compile, Ctrl+Z, then bg to continue it in the background while you work.
5. Why is quoting important when passing filenames to commands?
Show answer
The shell performs word splitting and glob expansion before running the command. Unquoted filenames with spaces, wildcards, or special characters will be split or expanded unexpectedly.Remember: "Quoting rules: double quotes preserve spaces but expand variables. Single quotes preserve everything literally."
🟡 Medium (8)¶
1. What are the main layers involved in terminal behavior?
Show answer
Terminal emulator, TTY/PTY driver (kernel), shell, and the running program. Each layer contributes different behavior, which is why terminal issues can be confusing.Remember: "Terminal layers: Emulator → TTY/PTY → Shell → Program." Debug by identifying which layer owns the problem.
2. What are the three standard streams and their file descriptor numbers?
Show answer
stdin = fd 0, stdout = fd 1, stderr = fd 2.Remember: "0-1-2: In-Out-Err." stdin=0, stdout=1, stderr=2. Redirect: 2>&1 sends stderr to stdout.
Example: command > output.txt 2>&1 captures both stdout and stderr to the same file.
3. When does the shell set up redirections — before or after the program starts?
Show answer
Before. The shell opens/creates files and adjusts file descriptors before exec-ing the program, so the program inherits the redirected descriptors.Gotcha: Redirections are processed left to right. 2>&1 > file.txt does NOT capture stderr to file — use > file.txt 2>&1 instead.
Remember: "Redirect the destination first, then point others to it."
4. What is the TERM environment variable for?
Show answer
It tells programs what terminal capabilities are available (colors, cursor movement, etc.). If TERM is wrong, curses-based programs like vim or top will misbehave.Gotcha: If TERM is wrong, colors and cursor movement break. Fix with: export TERM=xterm-256color.
Remember: "TERM tells apps what your terminal can do."
5. What are terminal escape codes?
Show answer
Special byte sequences (like ESC[31m for red text) that instruct the terminal emulator to change colors, move the cursor, clear the screen, or switch modes.Remember: "ESC[ starts most ANSI escape codes." ESC[31m = red, ESC[0m = reset. Programs use these to colorize output.
6. What does "stty sane" do?
Show answer
Resets the terminal line discipline to reasonable defaults, recovering from broken terminal settings (e.g., after a program crashes without restoring raw mode).Remember: "stty sane = terminal factory reset." Use it when your terminal is garbled after a program crash.
Example: If you accidentally cat a binary file and the terminal goes crazy: type stty sane and press Enter.
7. What happens to background jobs when you close a terminal?
Show answer
The kernel sends SIGHUP to all processes attached to that terminal session, which typically kills them. Use nohup, disown, or tmux/screen to prevent this.Gotcha: nohup only protects against SIGHUP. If the process reads from stdin, it may still die. Use tmux/screen for persistent sessions.
Remember: "tmux > nohup for anything interactive."
8. What does it mean when you see garbage like ^[[D in your terminal?
Show answer
An escape sequence is not being interpreted correctly. Either the terminal emulator doesn't support it, TERM is misconfigured, or the output is going to a non-terminal (like a file or pipe).🔴 Hard (2)¶
1. What is the difference between canonical mode and raw mode in the TTY driver?
Show answer
In canonical mode, input is line-buffered — the kernel collects a full line before sending it to the program. In raw mode, programs receive each character immediately, enabling interactive editors and REPLs.Remember: "Canonical = line mode (normal), Raw = character mode (editors)." Programs like vim switch to raw mode to read each keypress.
Gotcha: If a program crashes in raw mode, the terminal stays in raw mode — use stty sane or reset to fix.
2. When terminal behavior is weird, what should you check first?