rgoussu@goussu: ~/library/system-administration/exercises
~/library/system-administration/exercises cat build-your-own-shell.md

Build your own shell

# Implement a Unix shell from fork/exec up — pipes, redirection, job control, and signals — the process model's syscalls learned by wielding them.

Exercisesaved 2026-08-08source #linux#shell#processes#systems-programming#exercise#cli

Goal

Write a working interactive shell that runs external commands, wires pipelines, redirects I/O, and handles signals and background jobs. Building it turns the process model from theory into reflex: fork/exec/wait, file descriptors and dup2, process groups, and the signal-handling that makes Ctrl-C and job control work.

Subject: full brief & instructions

Practices

Milestones

  1. REPL + external commands. Read a line, tokenize it, fork + execvp the command, and waitpid for it. Handle the empty line and a not-found command cleanly.
  2. Built-ins. Implement cd, exit, and pwd — the ones that must run in the shell process itself (a forked cd changes nothing), which teaches why the distinction exists.
  3. Redirection. Parse >, >>, < and rewire stdin/stdout with open + dup2 before exec; confirm the child inherits the redirected descriptors.
  4. Pipelines. Support a | b | c: create pipes, fork each stage, wire read/write ends with dup2, close unused fds (the leaked-fd bug is a rite of passage), and wait on all.
  5. Signals & job control. Handle SIGINT/SIGTSTP so they hit the foreground job not the shell; add & background execution, process groups, and a jobs listing.

Stretch goals

  • Add environment-variable expansion, globbing, and quoting rules.
  • Add command history and line editing (readline or a raw-mode implementation).

Related