The Shell, the Terminal and the Kernel · about 30 min

Terminal, shell and kernel

Why this matters

Almost every server, container, router and cloud machine in the world is driven by text commands. A graphical tool can only do what its designer anticipated; a shell lets you combine small programs into exactly the tool you need, and then save that combination as a script that runs at 3 a.m. without you. Before writing any of that, you need a clear picture of what is actually happening when you type a command, because almost every confusing shell behavior you will meet later (quoting, globbing, "command not found", a variable that is empty when it "should not be") comes from not knowing which program is doing what.

Three layers: terminal, shell, kernel

When you sit at a command line, three different pieces of software are cooperating:

  you  ──keys──▶  TERMINAL  ──bytes──▶  SHELL (bash)  ──system calls──▶  KERNEL (Linux)
       ◀─pixels──  draws text ◀─bytes──  runs programs ◀──results──────   hardware, processes

A process is a running instance of a program: the program's code plus its own memory, its open files, its current directory, its user identity. When you run ls three times, the program /usr/bin/ls stays the same file on disk, but three separate processes are created and destroyed.

What happens when you press Enter

Here is the life of one command, ls /etc, in more detail than you need today. Keep it in mind; the whole course fills in these steps.

  1. bash prints a prompt (in this sandbox: learner@sandbox:/work$) and waits.
  2. You type ls /etc and press Enter. The terminal sends those characters to bash.
  3. bash parses the line: it splits it into words (ls and /etc), and performs expansions (variables, wildcards and so on; none here).
  4. bash looks up the first word. ls is not built into bash, so bash searches the directories listed in the PATH variable and finds the program file /usr/bin/ls.
  5. bash asks the kernel to fork: make a copy of the bash process. The copy (the child) then asks the kernel to exec /usr/bin/ls, which replaces the child's program with ls, handing it the words ls and /etc.
  6. ls runs. It asks the kernel to open the directory /etc, read its entries, and write their names to its output, which is connected to your terminal.
  7. ls exits with a small number, its exit status (0 means success).
  8. bash, which was waiting for the child, collects the status, and goes back to step 1.

That fork-then-exec pattern is how every external program on Unix is started. It explains a lot of later surprises; for instance, a program cannot change the current directory of the shell that started it, because it is running in a separate process.

Your first commands

Open the shell for this lesson and type along. Lines starting with $ are what you type (do not type the $); the other lines are output.

$ echo Hello
Hello
$ date
Sun Nov  1 14:03:12 UTC 2026
$ whoami
learner
$ pwd
/work
$ hostname
sandbox

Now make something:

$ mkdir first
$ ls
first
$ echo 'Hello, shell' > hello.txt
$ ls
first  hello.txt
$ cat hello.txt
Hello, shell

Notice that successful commands like mkdir print nothing at all. Unix tradition: silence means success. Errors are reported:

$ mkdir first
mkdir: cannot create directory 'first': File exists
$ cat nope.txt
cat: nope.txt: No such file or directory

Error messages almost always have the form program: what: why. Read them; they are usually exactly right.

The prompt and the command line editor

bash's line editor gives you a few keys worth learning on day one:

KeyEffect
Up / Down arrowsstep through previous commands (your history)
Tabcomplete a command or file name; press twice to list choices
Ctrl-Cabandon the current line, or interrupt a running program
Ctrl-A / Ctrl-Ejump to the start / end of the line
Ctrl-Rsearch history backwards: type part of an old command
Ctrl-Lclear the screen (same as the clear command)
Ctrl-Don an empty line: "end of input"; the shell exits

Tab completion is not just a convenience: it prevents typos in file names, and if Tab does not complete a name, that tells you the file is not where you think it is.

Note: This sandbox is a throwaway container with no network and no administrator (root) access. Your lesson workspace /work is kept between visits; everything else resets. You cannot damage anything, so experiment freely.

Common mistakes

Exercises

The two quizzes check the mental model; the two tasks happen in the sandbox shell. Task checks look at the files in /work, so the files are what matter, not the exact commands you typed.

Recap

Going further: run ps to see the processes in your sandbox, and echo $0 to see the name of your shell. Try ps -f and find bash and the ps process itself; the PPID column shows which process started which.

Exercises

Exercise 1 · quiz

Who reads the line?

You type ls /etc and press Enter. Which program reads that line of text, splits it into words and decides which program to start?

Answer
  • The shell (bash)

The shell. The terminal only passes your keystrokes along and draws whatever text comes back. The kernel never sees the text ls /etc as a command line at all: it only receives a request from bash to start the program /usr/bin/ls with the arguments ls and /etc. And ls is started after bash has already split the line, so it receives ready-made words.

Exercise 2 · quiz

Who does the real work?

Which statements are true? Select all that apply.

Answer
  • Only the kernel can create a new process or read a file from the disk.

  • When a command finishes, bash prints a new prompt and waits for the next line.

Programs, bash included, run in user space and must ask the kernel, through system calls, for anything that touches hardware or other processes: open a file, read bytes, start a process. cat asks the kernel to open and read notes.txt; it never drives the disk itself. And closing the terminal does matter: the kernel tells the shell its terminal went away (a signal called SIGHUP) and the shell normally exits. You will meet signals properly in a later module.

Exercise 3 · task in a shell

Your first two commands

Open the shell for this lesson. You start in /work, your workspace for this lesson.

  1. Create a directory named first with mkdir.
  2. Create a file named hello.txt (in /work, not inside first) that contains exactly the line Hello, shell using echo and >.

Then press Check.

Hint 1

mkdir first creates the directory. Run ls afterwards to see it.

Hint 2

echo 'Hello, shell' prints the text; adding > hello.txt sends it into a file instead of the screen.

Reference solution
mkdir first
echo 'Hello, shell' > hello.txt

Exercise 4 · task in a shell

Who and where are you?

In the shell, find out your user name with whoami and save its output in a file named me.txt. Then save the output of pwd (the directory you are in) in a file named where.txt. Both files belong in /work.

Hint 1

Run whoami on its own first and look at what it prints.

Hint 2

whoami > me.txt writes that same text into the file. Do the same with pwd.

Reference solution
whoami > me.txt
pwd > where.txt