How the Terminal and Shell Work
The terminal is your most powerful tool in Linux. But most people use it without really understanding what's happening. Let's demystify the terminal emulator, the shell, and the plumbing that connects programs together.
Terminal Emulator vs Shell — What's the Difference?
When you type ls -la and press Enter, here's what happens: the shell reads your input → finds the ls binary → forks a child process → executes ls with the arguments -la → waits for it to finish → shows the output.
stdin, stdout, stderr — The Three Streams
Every Linux process starts with three open file descriptors:
| Stream | FD Number | What it is | Default |
|---|---|---|---|
| stdin | 0 | Input the program reads | Your keyboard |
| stdout | 1 | Normal output | Your terminal |
| stderr | 2 | Error messages | Your terminal |
These are just files — that's the key insight. Redirecting them means pointing these file descriptors at different files or pipes.
Pipes and Redirection
cat /etc/passwd | grep root, the kernel creates an in-memory buffer (a pipe), cat writes to it, and grep reads from it — simultaneously. They run in parallel.
PATH — How the Shell Finds Commands
echo $PATH to see yours.
Adding a directory to PATH: export PATH="$HOME/.local/bin:$PATH". The new directory goes first, so your version takes priority over system versions.
Environment Variables
Environment variables are key-value pairs every process has. Child processes inherit the parent's environment. Common ones:
- HOME — your home directory (
/home/alice) - USER — your username
- SHELL — path to your shell (
/bin/bash) - EDITOR — default text editor
- LANG — locale/language setting
Shell Startup Files
~/.bashrc for interactive shells, ~/.bash_profile for login shells. For zsh: ~/.zshrc.
| File | When it runs | Use for |
|---|---|---|
| ~/.bashrc | Every new interactive bash shell | Aliases, functions, PS1 prompt |
| ~/.bash_profile | Login shells (SSH, console) | PATH, env vars |
| ~/.profile | Login shells (any shell) | Universal env vars |
| /etc/environment | System-wide, all users | Global env vars |
Frequently Asked Questions
What will I learn here?
This page covers the core concepts and techniques you need to understand the topic and progress confidently to the next lesson.
How should I use this page?
Start with the overview, then follow the section links to deepen your understanding. Use the table of contents on the right to jump to specific sections.
What should I read next?
Use the navigation below to continue to the next lesson or explore related topics.