Project 7: Environment Variable Manager

A complete variable system supporting shell variables, environment variables, variable expansion ($VAR, ${VAR}, ${VAR:-default}), and special variables ($?, $$, $!, $@, $#).

Quick Reference

Attribute Value
Primary Language C
Alternative Languages Rust, Go, Python
Difficulty Level 2: Intermediate (The Developer)
Time Estimate 1 week
Knowledge Area Operating Systems / Process Environment
Tooling Unix Shell
Prerequisites Projects 1-4, understanding of hash tables

What You Will Build

A complete variable system supporting shell variables, environment variables, variable expansion ($VAR, ${VAR}, ${VAR:-default}), and special variables ($?, $$, $!, $@, $#).

Why It Matters

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

Core Challenges

  • Distinguishing shell vs environment vars (export marks for inheritance) → maps to scoping
  • Variable expansion in context (no expansion in single quotes) → maps to evaluation rules
  • Special variables ($?, $$, $!, $0, $1, …) → maps to shell state
  • Parameter expansion operators (${var:-default}, ${var%pattern}) → maps to string manipulation
  • Word splitting after expansion ($var with spaces becomes multiple args) → maps to shell semantics

Key Concepts

  • Environment inheritance: “Advanced Programming in the UNIX Environment” Chapter 7.9 - Stevens
  • Parameter expansion: POSIX Shell Specification Section 2.6.2 - The Open Group
  • Special parameters: “Bash Reference Manual” Section 3.4.2 - GNU

Real-World Outcome

$ ./mysh
mysh> NAME="Douglas"
mysh> echo "Hello, $NAME"
Hello, Douglas
mysh> echo 'No expansion: $NAME'
No expansion: $NAME
mysh> echo ${NAME:-Anonymous}
Douglas
mysh> unset NAME
mysh> echo ${NAME:-Anonymous}
Anonymous
mysh> false
mysh> echo "Exit status: $?"
Exit status: 1
mysh> echo "Shell PID: $$"
Shell PID: 12345
mysh> export GREETING="Hi"
mysh> sh -c 'echo $GREETING'
Hi

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