Skip to content
← All projects
2021engineering

C-Shell

Custom Unix shell from scratch.

CPOSIX
C-Shell · case study

Context

Operating Systems coursework asked for a 'shell that handles a few commands.' I read the assignment, then read the bash source. The gap between those two felt like the actual lesson.

Problem

Implement a working Unix-like shell in C with proper process management — fork, exec, wait, signals, piping, redirection, history, and a small set of built-ins (cd, pwd, exit, history, kill).

Approach

Wrote the shell from scratch as a REPL with a hand-rolled tokenizer, a recursive-descent parser for command pipelines, and explicit syscall management. No external libraries beyond libc.

Build

  • Tokenizer that respected quoting, escaping, and operator boundaries.
  • Parser that built a small AST of pipeline nodes with redirection metadata.
  • Executor that forked per pipeline stage, wired up pipes via dup2, and waited correctly so background jobs didn't zombie.
  • Signal handlers for SIGINT / SIGTSTP / SIGCHLD that interacted nicely with the foreground job.
  • History file persisted across sessions, with up/down arrow navigation via raw mode terminal handling.

Outcome

Working shell that ran most common pipelines I threw at it. Top marks on the assignment. More importantly, I never look at `bash` the same way.

What I would change

I would have started with a real grammar definition. Hand-rolling the parser was educational but it's the part I'd most want to throw away if I touched the project again.

← Previous
Embedded Systems ML
Voltage prediction from amplifier circuit data.
Next →
Smart Railway System
Automated railway crossing prototype.