The command line, step by step: from a key to a process.
What a terminal and a shell are, how signals, jobs and sessions work, and what bash does with a line before the command starts: variables, quotes, globs and the PATH search.
9 chapters · 28 pages≈ 144 minstep-by-step simulators · demo terminal with jobs and missions
Your progress
Start with the terminal and the shell.
Follow the chapters in order or pick one on the map.
Study material generated with AI (Claude) from the slides of ISO lecture 04, to be reviewed by the lecturers. It may contain mistakes; for the exam, the reference is the lecture and its slides. Found a mistake? Open an issue on the course repository.
About this material
How it was made
Text, figures, the simulators and the demo terminal were generated with Claude (Anthropic) from the slides of lecture 04 – Command Line Interface, following the structure of the interactive DEEA modules. The diagrams of the slides were redrawn.
What was checked
The command line simulator follows the order of the bash expansions (braces, ~, $NAME, word splitting, globs, quote removal) and was tested against the examples of the slides. The demo terminal simulates the jobs (sleep really waits), the signals of the keys, the PATH search and the variables; its messages follow bash and GNU coreutils. Its process list is simulated.
Mistakes
If you find one, open an issue with the page and the sentence. Version: November 2026.
Sources and contributors
Slides: USO Team, Copyright (C) 2026 Wyliodrin & Politehnica Bucharest, CC-BY-SA 4.0. Bibliography: the sources listed in the slides of the lecture.
Every command you type goes through a terminal to a shell. They are two different programs, and knowing which one does what explains Ctrl+C, /dev/pts/0 and why closing a window can stop your programs.
one large computer, the mainframe, and many terminals: they send the keys and print what the computer sends back
the old terminals were TeleTypes, TTY: a keyboard and a printer, later a screen, but no processor
everything ran on the mainframe, the terminals only moved text in and out
Linux still calls terminals tty: /dev/tty1, /dev/tty2, ... are files in /dev
Text mode
Virtual terminals: /dev/tty1 ... /dev/tty6
Your computer has one screen and one keyboard, but the kernel offers several virtual terminals on them. At start-up, a program waits for a login on each one; after the login, it becomes your shell.
The graphical interface uses a terminal too
The display server (Xorg or Wayland) takes over tty1 or tty2 and draws through the framebuffer, /dev/fb0. Ctrl+Alt+F3 ... F6 switch to a text terminal, Ctrl+Alt+F1 or F2 back.
The terminal emulator and the shell
Terminal and shell: two programs
A shell is a process whose main job is to let you talk to the operating system. In a graphical session, its text is shown by a terminal emulator, another program.
foot (or kitty, ptyxis, alacritty) is the window; bash (or zsh, fish) is what runs inside it
$tty # the terminal of the shell/dev/pts/0$ps # the processes of this terminal PID TTY TIME CMD 4100 pts/0 00:00:00 bash 4300 pts/0 00:00:00 ps
Kind of shell
Examples
command line
bash, sh, zsh, fish, cmd, powershell
text user interface
mc, nnn, yazi
graphical
Explorer, Finder, nautilus, the Settings
Check yourself
You close the window of foot. Which program ended first?
✉️ A number from the kernel
Signals: a message for a process
A signal is a short message the operating system sends to a process: just a number (sometimes with one more number as payload). Ctrl+C, closing a window and kill all send one; kill -l lists them. When it arrives, the process runs the default action of the signal, or its own handler (next page).
Exit code 128 + the signal number
A process ended by a signal has the exit code 128 + N: 130 after Ctrl+C (2), 137 after kill -9, 143 after kill (15).
Handling signals
trap: your own handler
When a signal arrives, the process stops what it does and runs the default action, or, if it registered one, its own signal handler; after a handler it goes on with its work. In bash: trap FUNCTION SIGNAL.
SIGKILL and SIGSTOP can never be caught, blocked or ignored: the last resort when a program does not listen
trap '' SIGINT (an empty handler) ignores the signal
programs use handlers to clean up: delete temporary files, save the work, then exit
Check yourself
A script has trap cleanup SIGTERM. Which command still ends it, with no cleanup?
One in front, any number behind
Foreground and background
Why it matters
A command that takes minutes (a download, a build, a server) does not have to block your terminal: put it in the background and keep working. The same keys and commands bring it back.
cmd runs in the foreground, cmd & in the background; Ctrl+Z stops the foreground process, bg and fg continue it
Foreground
only one process (or pipeline)
it reads the keyboard (stdin)
the shell waits for it
Background
any number of processes, called jobs
running or stopped
the shell does not wait: it shows the prompt
Run in the background
&: start a job
Add & at the end: the shell starts the command and shows the prompt right away. It prints the job ID and the PID.
Common mistake
A job still writes to your terminal
ls -R / & fills the screen while you type: the background only frees the keyboard, not the display.
Instead: redirect the output of a job: find / -name '*.log' > logs.txt 2> /dev/null &.
From the foreground to the background and back
Ctrl+Z, bg, fg: move a job
Forgot the &? Stop the foreground process with Ctrl+Z, then let it continue in the background with bg. fg brings a job back to the foreground.
Jobs in the demo terminal
Cheat sheet and practice
You type
What happens
Signal
cmd &
starts a job in the background: [1] 4300
jobs
the jobs of this shell: + current, - previous
Ctrl+Z
stops the foreground process, it becomes a job
SIGTSTP
Ctrl+C
ends the foreground process
SIGINT
bg %1
job 1 continues in the background
SIGCONT
fg %1
job 1 comes to the foreground
SIGCONT
kill %1
ends job 1
SIGTERM
kill -9 %1, kill -KILL 4300
ends it, no way to refuse
SIGKILL
In this terminal sleep really waits. Ctrl+C, Ctrl+Z and Ctrl+\ work as keys and as buttons; the panel shows the session, one line per process group.
How the kernel groups the processes of a terminal
Sessions, groups and the controlling terminal
The kernel groups the processes of a terminal in two levels: a process group is one job, a session is all the groups of one terminal. One group is in the foreground: only it gets the keys.
SIGKILL cannot be caught
When bash is killed
Normally bash forwards SIGHUP to its jobs when the terminal closes. If bash is killed with SIGKILL, it cannot forward anything.
Check yourself
You start nohup ./backup.sh > backup.log & and close the terminal. What happens to the script?
Keys and signals
Ctrl+C, Ctrl+\ and Ctrl+Z
The terminal driver in the kernel turns some keys into signals for the whole foreground group.
Keys
Sends
Default action
Ctrl+C
SIGINT
terminate (interrupt) the process
Ctrl+Z
SIGTSTP
stop the process, it becomes a stopped job
Ctrl+\
SIGQUIT
terminate it and write a core dump
Ctrl+D
no signal
end of file: stdin has nothing more to read
Only the foreground reads the keyboard
A job that tries to read the keyboard gets SIGTTIN and is stopped. bg and fg send SIGCONT to continue a stopped job.
Every process has one
The environment: NAME=value texts
every process has a list of NAME=value texts: its environment
a process receives it from its parent: fork copies it, exec keeps it
programs read it to configure themselves: getenv("HOME") in C, $HOME in bash
the names are in UPPER CASE, by convention
$printenv HOME # one variable/home/student$echo"I am $USER" # the shell replaces $USERI am student$env # all of them
The shell works on the line in a fixed order: it splits it into words, sets the assignments and redirects apart, replaces the variables, splits the results, expands the globs, removes the quotes, and only then starts the command.
From left to right
The order of redirects
The shell applies the redirects from left to right. 2>&1 means: 2 goes where 1 goes now. It is a copy, not a link: changing 1 later does not change 2.
$ls /etc /nope > all.txt 2>&1 # nothing on the display$ls /etc /nope 2>&1 > all.txt # the error is on the displayls: cannot access '/nope': No such file or directory
Quoting and escaping
Quotes and \: what the command receives
Spaces, $, * and the quotes are special for the shell. The quotes and the \ tell it to treat them as plain characters; then the shell removes them: the command only gets the words.
'...' single quotes: nothing inside is special
"..." double quotes: $NAME is replaced, the result stays one word, the spaces are kept
\ before a character: that one character is plain
Globbing
Globs: patterns for file names
An unquoted *, ? or [...] is a pattern: the shell replaces it with the names of the matching files, sorted, before the command starts. The command never sees the pattern.
Common mistake
Quote a pattern that is for the command
find . -name *.txt is expanded by the shell first: find gets -name fruits.txt my list.txt ... and fails.
Instead:find . -name '*.txt': the quotes keep the * for find.
Quotes and globs in the demo terminal
Cheat sheet and practice
Write
The command gets
Why
echo $NAME
AliceSmith
replaced, then split
echo "$NAME"
Alice Smith
one word
echo '$NAME'
$NAME
nothing replaced
echo \$NAME
$NAME
the $ is plain
echo *.txt
the .txt files
glob
echo '*.txt'
*.txt
a quoted pattern stays
echo {1..3}
123
braces, files or not
Not every command is a program
Built-in and external commands
Built-in commands
run inside the shell, no new process
cd, export, unset, jobs, fg, bg, type, exit, echo
External commands
executable files, somewhere on the disk
the shell needs their path
ls, sort, grep, bash
$type cdcd is a shell builtin$type sortsort is /usr/bin/sort
Check yourself
Why must cd, unset and exit be built into the shell?
How the shell finds the program
Which file? A path, a builtin or a PATH search
For an external command the shell needs a path. A first word with a / is a path already; a plain name is looked up in the directories of the PATH variable, from left to right.
🐧 exec only starts a path: turning a name into a path is the job of the shell (of the C library, in fact: the shell calls execvp).
The PATH in the demo terminal
which, type and practice
$which sort # the PATH search, without running sort/usr/bin/sort$which-a sort # every match, in the order of PATH/usr/bin/sort/bin/sort$which cd # nothing found: no output$echo $?1
which prints the file the shell would run
-a prints every match
which is an external command: it only searches PATH, it does not know the builtins; type cd asks the shell itself
the current directory is not in PATH: that is why you run ./hello
Quick recap
The ideas, in short
One idea per card; the title is the idea, the text says why it matters in practice. Recap shows all the cards, Flashcard one at a time.
01 · Terminal
A terminal moves text, the computer runs the programs
click / Space
01 · Terminal
A terminal moves text, the computer runs the programs
The old TTYs had no processor; Linux still names terminals /dev/tty1, /dev/pts/0.