Project 4: Built-in Commands Engine

An extensible system for shell built-in commands (cd, pwd, exit, export, unset, alias, source, history) that run within the shell process rather than as child processes.

Quick Reference

Attribute Value
Primary Language C
Alternative Languages Rust, Go, Zig
Difficulty Level 2: Intermediate (The Developer)
Time Estimate 1 week
Knowledge Area Operating Systems / Shell Design
Tooling Unix Shell
Prerequisites Project 1, understanding of environment variables

What You Will Build

An extensible system for shell built-in commands (cd, pwd, exit, export, unset, alias, source, history) that run within the shell process rather than as child processes.

Why It Matters

This project builds core skills that appear repeatedly in real-world systems and tooling.

Core Challenges

  • Identifying built-ins before fork (lookup in built-in table) → maps to command dispatch
  • Implementing cd correctly (chdir + PWD update + OLDPWD) → maps to process properties
  • Implementing export (modifying environment for children) → maps to environment inheritance
  • Implementing source/dot (executing script in current shell) → maps to execution context
  • Making it extensible (easy to add new built-ins) → maps to software design

Key Concepts

  • Why cd is built-in: “Advanced Programming in the UNIX Environment” Chapter 4.22 - Stevens
  • Process environment: “The Linux Programming Interface” Chapter 6 - Kerrisk
  • Shell variables: “Shell Scripting Expert Recipes” Chapter 5 - Steve Parker

Real-World Outcome

$ ./mysh
mysh> pwd
/home/douglas
mysh> cd /tmp
mysh> pwd
/tmp
mysh> echo $OLDPWD
/home/douglas
mysh> export MY_VAR="hello"
mysh> /bin/sh -c 'echo $MY_VAR'
hello
mysh> cd -
/home/douglas
mysh> exit 0
$ echo $?
0

Implementation Guide

  1. Reproduce the simplest happy-path scenario.
  2. Build the smallest working version of the core feature.
  3. Add input validation and error handling.
  4. Add instrumentation/logging to confirm behavior.
  5. Refactor into clean modules with tests.

Milestones

  • Milestone 1: Minimal working program that runs end-to-end.
  • Milestone 2: Correct outputs for typical inputs.
  • Milestone 3: Robust handling of edge cases.
  • Milestone 4: Clean structure and documented usage.

Validation Checklist

  • Output matches the real-world outcome example
  • Handles invalid inputs safely
  • Provides clear errors and exit codes
  • Repeatable results across runs

References

  • Main guide: SHELL_INTERNALS_DEEP_DIVE_PROJECTS.md
  • “Advanced Programming in the UNIX Environment” by W. Richard Stevens