You type ls, press enter, and a program runs. The shell that made that happen feels like deep operating-system magic. It isn't. A real, working shell, one that can run any program on your system, is about 30 lines of C, and writing it teaches you the single most important pattern in operating systems: how one program starts another.

That pattern is three system calls: fork, exec, and wait. Every program you have ever launched, from your editor to your browser, was started this way.

The one idea: fork then exec

To run a program, a Unix shell does something that sounds strange the first time: it clones itself, and then the clone replaces itself with the program you asked for.

fork() creates a near-identical copy of the current process (the child). Now there are two processes running the same code.