Introduction
A shell is the program you talk to in every terminal. Writing one teaches parsing, state management and how programs launch other programs. kenholt-sh implements the core loop of bash in a few hundred readable lines.
Problem statement
$NAME and $?, keeps a 20-command history with !N recall, implements built-ins (echo, set, calc, cd, pwd, history, help, exit) and passes any other command to the operating system. It must work interactively and from a script on standard input.Objectives
- Write a tokeniser that handles quotes and escapes
- Implement variable expansion before tokenising
- Manage a fixed-size circular history
- Dispatch built-ins and fall back to system()
Features
- Quotes: "…" (with expansion) and '…' (literal)
- Variables with validated names and $? for the last status
- History with !N and an error for unknown events
- Built-ins plus external commands via system()
- Portable cd/pwd on Windows and POSIX
Concepts demonstrated
- Lexical analysis
- State machines
- Circular buffers
- Process exit status
- Conditional compilation
System architecture
main.c runs the loop: read a line, handle !N, call shell_parse, record history, try run_builtin, otherwise rebuild the command and call system(). parser.c first expands variables into a buffer, then splits it into argv in place, tracking quote state. builtins.c implements each command and updates last_status.
Folder structure
Complete source code
Makefile
What each file does
- src/shell.h
- Shell state (history ring, variables, status) and the parser/built-in API.
- src/parser.c
- Variables,
$expansion and the quote-aware tokeniser. - src/builtins.c
- echo, set, calc, cd, pwd, history, help and exit; history recording.
- src/main.c
- The REPL,
!Nrecall and delegation to the system shell. - Makefile
- Build automation:
makebuilds./app,make testruns the sample session and compares it with the expected output,make cleanremoves build files.
How to run it
Build
Run
Test against the expected output
Expected output
Generated by running the project with tests/sample-input.txt. Your output should match tests/expected-output.txt exactly.
help echo Hello from kenholt-sh set NAME=Asha set COURSE="C Programming" echo "Welcome, $NAME, to $COURSE" echo 'single quotes keep $NAME literal' calc 12 x 7 calc 10 / 0 echo last status was $? set 1bad=x echo "unterminated history !3 !99 exit
Testing
./app < tests/sample-input.txtmust matchtests/expected-output.txt(built-ins only, so it is identical on every OS).- Interactively run
ls(Linux/macOS) ordir(Windows) to see delegation. - Try
echo "unterminatedand check the syntax error.
Common errors
Variables expand inside single quotes
expand tracks single-quote state and copies $ literally while inside them.
External commands with spaces in arguments fail
join_args re-quotes arguments that contain spaces before calling system().
Possible improvements
- Pipes (
a | b) and redirection (> file) using fork/exec/pipe on POSIX - Tab completion
- Aliases
Additional challenges
- 1Implement
if/then/fifor simple scripts. - 2Run commands with fork/execvp/waitpid instead of system() (POSIX).
- 3Add
exportso variables are passed to child processes.
Built and tested it yourself? Mark it to update your dashboard.