Module 1 · Lesson 3 of 31

The kernel, the terminal and the shell

How programs reach the kernel through system calls, what separates the terminal, the shell and the console, and what happens when you press Enter.

  • Beginner to advanced
  • 11 min read
  • 20 min lab
What you will need

Ubuntu 24.04 (WSL2, a VM, or a container) with a terminal; no special hardware

By the end you can 3 objectives
  • Describe the relationship between the kernel, user space and a distribution, naming an example of each.
  • Distinguish the terminal, the shell and the console by stating the role of each.
  • Trace how a shell finds and runs a command (lookup, then fork, exec and wait) and explain why cd must be a builtin.
Read the lesson below; the video will appear here when published.
01

The kernel, user space, and the system call

2 min read

A running system has two halves. holds everything you launch: the shell, editors, browsers, your own code. kernel space holds the kernel itself, its drivers and its scheduler. A program in user space cannot read another process's memory, write to the disk directly, or talk to a peripheral on its own.

The controlled way across this boundary is a . When a program wants to read a file, it does not open the disk controller; it calls the read() system call, and the kernel performs the operation on its behalf, copying the result back into the program's memory. There are a few hundred system calls, and everything a program can do reduces to them.

Kernel space and user spaceUser spacebash, ls, your programsC libraryglibc or muslSystem callsKernelprocesses, memory, driversHardwareCPU, RAM, diskrequests go down, data comes upUser spacebash, ls, your programsC libraryglibc or muslSystem callsKernelprocesses, memory, driversHardwareCPU, RAM, diskrequests go down, data comes up
Layers of a running system: requests travel down through system calls, data comes back up

This is also why the strict name is GNU/Linux, not Linux. The Linux kernel alone cannot run your programs; you also need user-space tools such as the C library, the shell and the core utilities, most of which came from the GNU project. Both halves are open source, but they are different projects with different maintainers.

The split is enforced by hardware, not by convention. CPUs offer several privilege levels (four rings on x86, exception levels EL0 to EL3 on 64-bit ARM), and Linux uses two of them: kernel code runs at the privileged level (ring 0, EL1) and user code at the unprivileged one (ring 3, EL0). The system call is the single, checked doorway that switches between them, and a system call costs far more than a normal function call, which is why the C library buffers file I/O for you.

02

The terminal, the shell, and the console

2 min read

A is what you type into. Originally terminals were physical teletype machines wired to a computer; today a terminal emulator such as GNOME Terminal, Konsole, or the Windows Terminal hosts your Linux session and simply draws text and sends keys.

A is the program the terminal is attached to. Bash is the default shell on most distributions; zsh and fish are common alternatives, and BusyBox's ash is the tiny shell you will meet on embedded boards.

A is the machine's own surface. On a server, the console is where the kernel prints early boot messages. On an embedded board, the console is usually a serial port: you connect a USB-to-serial cable, open a terminal at 115200 bits per second, and watch the boot log scroll past before a login prompt appears.

On a desktop Linux, the console is the set of virtual terminals: text logins you reach with Ctrl+Alt+F1 through F6, while the graphical desktop usually lives on F1 or F7 depending on the distribution. Each runs a getty process that prints a login prompt. When you log in there, the system reads your entry in /etc/passwd, whose seventh and last field names your login shell, and starts that shell for you.

Terminal, shell and consoleTerminalShellConsoleWhat it isa window ordevicea programa physicalportJobdraws text,sends keysinterpretscommandsboot log andloginExampleGNOMETerminal,tty1bash, zshserialconsole, VT1
Three distinct things that share a name: terminal, shell and console
03

The shell: your command interpreter

2 min read

After you log in, the shell that starts is your login shell. A login shell reads the system-wide /etc/profile and then your own ~/.profile (on Debian-family systems that file in turn sources ~/.bashrc). A new terminal window you open later runs an interactive shell that is not a login shell; it reads ~/.bashrc instead. A shell running a script is non-interactive and reads none of those by default. This is why an alias you defined in ~/.bashrc is available in your terminal but not inside a script.

Two things tell you which shell you have, and they are not the same. The environment variable $SHELL, set at login, records what your login shell was and is what new programs consult. Your actual login shell is the last field of your /etc/passwd entry. Because $SHELL can be changed by anything, it is a hint, not proof.

bash
echo $SHELL        # typical output: /bin/bash
getent passwd "$USER"   # typical output ends with :/bin/bash

A handful of commands identify you and the machine. whoami prints your username, id prints your user and group IDs, hostname prints the machine name, and uname -a prints kernel and hardware details.

bash
id        # typical output: uid=1000(learner) gid=1000(learner) groups=1000(learner),...
uname -a  # typical output: Linux labpc 6.8.0-45-generic #45-Ubuntu SMP ... x86_64 x86_64 x86_64 GNU/Linux

The output above is typical; yours will differ in the hostname, version and architecture. On an ARM board the architecture line reads aarch64 or armv7l, a reminder that the same kernel runs on very different hardware.

Three kinds of shellLogin shellInteractiveshellScriptWhen it startsat loginnew terminalwindowa script runsFiles read/etc/profile,~/.profile~/.bashrc$BASH_ENV ifsetTypical useset PATH,umaskaliases,promptautomation
Login, interactive and non-interactive shells read different startup files
Who you are and what this machine is
learner@embeddex:~/lab$ # your username
learner@embeddex:~/lab$ whoami
learner
learner@embeddex:~/lab$ # user and group IDs
learner@embeddex:~/lab$ id
uid=1000(learner) gid=1000(learner) groups=1000(learner),4(adm),24(cdrom),27(sudo),30(dip),46(pl
ugdev),100(users)
learner@embeddex:~/lab$ # the machine's name
learner@embeddex:~/lab$ hostname
embeddex
learner@embeddex:~/lab$ # kernel and hardware details
learner@embeddex:~/lab$ uname -a
Linux embeddex 6.18.58.1-microsoft-standard-WSL2 #1 SMP PREEMPT_DYNAMIC Tue Oct 27 00:24:11 UTC
2026 x86_64 x86_64 x86_64 GNU/Linux
learner@embeddex:~/lab$ # your login shell, as recorded at login
learner@embeddex:~/lab$ echo $SHELL
/bin/bash
learner@embeddex:~/lab$ # the same answer from /etc/passwd, the source of truth
learner@embeddex:~/lab$ getent passwd "$USER"
learner:x:1000:1000:,,,:/home/learner:/bin/bash
04

What happens when you press Enter

5 min read

When you type ls -la /etc and press Enter, the terminal delivers the line to the shell, which runs it through a fixed pipeline.

First the shell parses the line into words: the command name ls and the arguments -la and /etc. A command name can be an alias, a function, a builtin, or an external program, and the shell checks them in that order: alias, then function, then builtin, then a cache of previously found programs, then the directories in $PATH. If nothing matches, the shell prints command not found and sets the exit status to 127.

Step by stepThe fork-exec-wait pipeline
  1. Read the line: the terminal sends the keystrokes to the shell, which waits for Enter.

  2. Parse: the shell splits the line into words, the command name first.

  3. Look up: the shell decides what the name means, in the order alias, function, builtin, hashed program, PATH.

  4. Fork: for an external command, the shell asks the kernel to copy itself, creating a child process.

  5. Exec: the child replaces itself with the program, loading its code and starting it.

  6. Run: the program executes, writing output to the terminal and an exit status when it finishes.

  7. Wait: the parent shell waits for the child and collects its exit status.

  8. Prompt again: the shell prints a new prompt; the status is now available as $?.

A is what the shell creates when it forks. fork() duplicates the shell; execve() replaces the duplicate with the new program; wait() lets the parent wait for the child to finish. Because these three calls run every command you type, they are worth seeing once in their raw form.

Going deeperWhat the shell really calls

The shell is just a program, and you can watch it with strace, which prints each system call a process makes. Run strace -f -e trace=process bash -c 'ls /; true' and you will see clone (how the C library implements fork() on modern Linux), then execve and wait4 scroll past. The ; true matters: given a single command, bash skips the fork and simply replaces itself with ls, an optimisation you would otherwise mistake for a missing step. In C, the same three steps look like this, with error handling omitted for brevity:

c
#include <unistd.h>
#include <sys/wait.h>

int main(void) {
    pid_t pid = fork();
    if (pid == 0) {
        char *args[] = {"/bin/echo", "hello", NULL};
        execve("/bin/echo", args, NULL);
        return 1;              /* reached only if execve fails */
    }
    int status;
    waitpid(pid, &status, 0);  /* parent waits for the child */
    return 0;
}

The child calls execve to replace itself with /bin/echo; the parent blocks in waitpid until the child finishes.

This is why cd must be a builtin. A process's working directory is a property of that process. If cd were a separate program, it would change its own directory and exit, and your shell would be exactly where it was. So the shell implements cd itself, and builtins such as cd, type and export run inside the shell without any fork.

The of the last command is stored in the special variable $?.

bash
nosuchcommand; echo $?   # typical output: command not found, then 127
ls /nope; echo $?        # typical output: an error, then 2

Exit status 0 means success, 127 means command not found, 126 means found but not executable, and 2 is what most programs return for a usage or access error. This is how scripts decide what to do next, and how you tell a failure from a mistake.

fork, exec and waitShellKernelProgramfork()child pidexecve(/bin/ls)run /bin/lswrites listingexit(0)waitpid returns1. Shell → Kernelfork()2. Kernel → Shellchild pid3. Shell → Kernelexecve(/bin/ls)4. Kernel → Programrun /bin/ls5. Program → Shellwrites listing6. Program → Kernelexit(0)7. Kernel → Shellwaitpid returns
The system calls behind one Enter: fork, execve and waitpid
Exit statuses: 0, 127 and 2
sbin
sbin.usr-is-merged
snap
srv
sys
tmp
usr
var
learner@embeddex:~/lab$ # $? holds the last exit status; 0 means success
learner@embeddex:~/lab$ echo $?
0
learner@embeddex:~/lab$ # the shell cannot find this name
learner@embeddex:~/lab$ nosuchcommand
bash: nosuchcommand: command not found
learner@embeddex:~/lab$ # 127 means command not found
learner@embeddex:~/lab$ echo $?
127
learner@embeddex:~/lab$ # found, but the path does not exist
learner@embeddex:~/lab$ ls /nope
ls: cannot access '/nope': No such file or directory
learner@embeddex:~/lab$ # 2 means it ran but failed on its input
learner@embeddex:~/lab$ echo $?
2
cd is a builtin, and type proves it
learner@embeddex:~/lab$ # cd runs inside the shell: a builtin
learner@embeddex:~/lab$ type cd
cd is a shell builtin
learner@embeddex:~/lab$ # ls is an external program on disk
learner@embeddex:~/lab$ type ls
ls is /usr/bin/ls
learner@embeddex:~/lab$ # echo is a builtin too
learner@embeddex:~/lab$ type echo
echo is a shell builtin
learner@embeddex:~/lab$ # type -a lists every match; the builtin wins
learner@embeddex:~/lab$ type -a echo
echo is a shell builtin
echo is /usr/bin/echo
echo is /bin/echo

Key takeaways

  • User space can reach hardware only through system calls; that is the whole security model in one sentence.
  • The terminal draws text and sends keys, the shell interprets commands, and the console is the machine's own login surface.
  • Pressing Enter makes the shell parse, look up, fork, exec and wait; cd is a builtin because a child cannot change its parent's directory.
  • Exit status 0 means success, 127 means not found, and 2 usually means a usage or access error.

Reflect

  1. Explain to a colleague, in two sentences: describe the relationship between the kernel, user space and a distribution, naming an example of each.
  2. Which objective are you least sure about? Re-read that section, then do its exercise in the practice pack.
  3. What surprised you when you ran the commands yourself?

On a real board

Repeat the key commands on your Raspberry Pi or lab board over SSH or the serial console. Note anything that behaves differently from your PC, such as missing tools, a BusyBox shell or a read-only filesystem.

Practise this lessonExercises, quiz, interview preparation and spaced review

References and further reading

Official documentation first. Links checked when the lesson was published.

Find your next step

↑ ↓ to move · Enter to open · Esc to close