Linux System Programming · advanced · ~20 min

Mini shell project

Combine `fork`, `exec`, and `wait` to build a tiny REPL shell. (A REPL is a Read-Eval-Print Loop: read input, run it, show the result, repeat.)

Lesson

The shell loop

A shell repeats the same four steps over and over:

  1. Read a line of input.
  2. Parse that line into argv (an array of argument strings).
  3. fork + exec to launch the requested program.
  4. wait for that program to finish.

A usable subset fits in about 60 lines of C. "Subset" here means the basics only: no pipes (|) and no redirections (>, <).

Why build one

Writing a mini shell is the classic rite of passage for Linux systems programming. It ties together three core ideas:

  • fork — the split into a parent process and a child process.
  • exec — replacing the child's program image with a new one.
  • wait / exit — the contract by which a parent collects a child's exit status.

Common mistakes

  • Not handling EOF (Ctrl-D) on stdin. When input ends, your shell should exit cleanly instead of looping forever.
  • Tokenising incorrectly. The argv array you pass to exec must be NULL-terminated, or exec will read past the end of the array.

Practice with these exercises