A POSIX-compliant shell in modern C++, built for the
CodeCrafters "Build Your Own Shell" challenge.
Built directly on POSIX syscalls (fork, execvp, pipe, dup2) with GNU
readline for line editing and tab completion. My first take on the challenge; I
later rewrote it in Rust.
- Builtins:
echo,pwd,cd(with~expansion),type,history,exit [status] - External commands via
PATHlookup +fork/execvp - Quoting: single quotes, double quotes, and backslash escaping, following the
POSIX rule that inside double quotes
\only escapes",\,$and` - Redirection:
>,1>,>>,1>>for stdout,2>,2>>for stderr - Pipelines with
|: every stage forked, the parent waits for all of them - Tab completion of builtins and all
PATHexecutables (GNU readline) - History:
history [n],history -r/-w/-a <file>, loads$HISTFILEon startup and appends new entries on exit
$ ./your_program.sh
$ echo 'hello world' | tr a-z A-Z > /tmp/out.txt
$ history 2- How real shells are structured: builtins like
cdandexitmust run inside the shell process itself, while external commands run in forked child processes - How redirection and pipes work under the hood: file descriptors,
dup2, andpipe - Signal handling: why Ctrl-C terminates the running command but not the shell itself
- Adding line editing and tab completion with GNU readline
| File | Role |
|---|---|
src/main.cpp |
The entire shell: REPL + completion (main, readline), quoting + pipeline parsing (ArgsParser), redirection setup (Command), builtins (handle_*), fork/exec + fd wiring (run_external, setup_stdio) |