A Unix shell implementation written in C, supporting piping, an interactive history browser, and standard builtins. Built from scratch using POSIX system calls.
- Command execution — runs any program available on
$PATHviaexecvp - Piping — chains commands with
|, supporting multiple pipes (e.g.ls | grep foo | wc -l) - Interactive history browser — press
Ctrl+\to enter history mode, browse with arrow keys, select with Enter - Builtins —
cd,exit, andhelp - Signal handling —
Ctrl+Ckills the foreground process without terminating the shell,SIGTERMrestores terminal state before exiting - Input preprocessing — automatically normalises spacing around
|sols|grep fooworks as expected - Dynamic argument lists — argument buffers grow automatically via
realloc, no hardcoded limits
makeRequires GCC and GNU Make.
./shell>> ls -la # run any program
>> ls | grep foo | wc -l # pipe commands
>> cd ~/projects # change directory
>> exit # exit the shellPress Ctrl+\ to enter history mode:
↑/↓— browse historyEnter— execute selected commandq— return to normal mode
Duplicate consecutive commands are not added to history.
shell/
├── main.c # main loop
├── parse.c/.h # tokenisation and input preprocessing
├── execute.c/.h # forking, piping, execvp
├── history.c/.h # history linked list and interactive browser
├── builtins.c/.h # builtin command dispatch table
├── signals.c/.h # signal handler setup and handlers
├── globals.c/.h # shared global state
└── Makefile
- Commands are stored as a linked list of
linked_cmdsnodes, each holding aNULL-terminatedchar**argument array — the formatexecvpexpects directly - Piping is implemented by creating a
pipe()before each piped command, usingdup2to wire stdout to the write end in the child and stdin to the read end in the next child - History is a doubly linked list to support both forward and backward browsing
- History mode uses raw terminal mode (
termios) to read keypresses without waiting for Enter SIGQUIT(Ctrl+\) sets avolatile sig_atomic_tflag that thegetlineloop checks afterEINTRto enter history mode safely