Skip to content
Tech News
← Back to articles

Building a Shell

read original get Shell Building Kit → more articles
Why This Matters

This article explores the process of building a simple Unix shell, highlighting its importance in understanding command execution and user interaction in the tech industry. For consumers and developers alike, grasping how shells work under the hood can lead to more efficient workflows and deeper insights into system operations.

Key Takeaways

The shell sits in front of a lot of my work, but I mostly use it for the outcome: running unix commands and scripts, creating branches and making commits. Unlike when I'm writing code, I'm rarely thinking about how the shell itself works under the hood.

So, to dig a bit deeper into shells, I'm going to build a toy one until I run out of time. I have a fresh pot of filter coffee, and I'm awake three hours before everyone else.

A quick look ahead to everything I'm able to support by the end:

./andsh andsh$ cd / andsh$ pwd / andsh$ echo $HOME /Users/andrew andsh$ nosuchcommand nosuchcommand: No such file or directory andsh$ echo $? 127 andsh$ printf abc

| tr a-z A-Z | rev CBA andsh$ ec<Tab> hello andsh$ echo hello hello andsh$ <Up> andsh$ echo hello hello andsh$ ^D

If you prefer reading C over prose, head straight to healeycodes/andsh.

REPL

A shell is an interactive program before it's a language implementation, and the user experience starts at the prompt. This first step is about building the interactive skeleton: print a prompt, read a line, keep a little state, and leave a clean place to plug executio logic into.

// repl.h typedef struct { int last_status ; int running ; int interactive ; } Shell ;

We also need the classic read-eval-print loop:

... continue reading