Project 12: History System

A command history system with navigation (up/down arrows), search (Ctrl+R), persistence across sessions, and history expansion (!!, !$, !-2, !string).

Quick Reference

Attribute Value
Primary Language C
Alternative Languages Rust, Go, Python
Difficulty Level 2: Intermediate (The Developer)
Time Estimate 1 week
Knowledge Area Data Structures / Persistence
Tooling Shell History
Prerequisites Project 11 (or use readline), basic data structures

What You Will Build

A command history system with navigation (up/down arrows), search (Ctrl+R), persistence across sessions, and history expansion (!!, !$, !-2, !string).

Why It Matters

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

Core Challenges

  • Circular buffer (fixed-size history with wrap-around) → maps to data structures
  • History navigation (integrating with line editor) → maps to component integration
  • History search (reverse incremental search) → maps to search algorithms
  • History file format (timestamped entries, shell compatibility) → maps to file formats
  • History expansion (parsing !! and friends before execution) → maps to preprocessing

Key Concepts

  • History facilities: “Bash Reference Manual” Section 9 - GNU
  • History expansion: “Bash Reference Manual” Section 9.3 - GNU
  • Circular buffers: “Algorithms in C” Chapter 4 - Sedgewick

Real-World Outcome

$ ./mysh
mysh> echo hello
hello
mysh> echo world
world
mysh> !!                    # Re-run last command
echo world
world
mysh> echo !$               # Use last argument
echo world
world
mysh> !echo                 # Run last command starting with 'echo'
echo world
world
mysh> history
    1  echo hello
    2  echo world
    3  echo world
    4  echo world
    5  echo world
mysh> ^R                    # Ctrl+R for search
(reverse-i-search)`hel': echo hello

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
  • “Bash Reference Manual” by GNU