Skip to content

About

My C++ implementation of the "Build Your Own Shell" CodeCrafters Challenge

Resources

Stars

1 star

Watchers

0 watching

Forks

Repository files navigation

Build Your Own Shell - C++

progress-banner

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.

What it supports

  • Builtins: echo, pwd, cd (with ~ expansion), type, history, exit [status]
  • External commands via PATH lookup + 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 PATH executables (GNU readline)
  • History: history [n], history -r/-w/-a <file>, loads $HISTFILE on startup and appends new entries on exit
$ ./your_program.sh
$ echo 'hello world' | tr a-z A-Z > /tmp/out.txt
$ history 2

What I learned

  • How real shells are structured: builtins like cd and exit must 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, and pipe
  • Signal handling: why Ctrl-C terminates the running command but not the shell itself
  • Adding line editing and tab completion with GNU readline

Skeleton

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)

About

My C++ implementation of the "Build Your Own Shell" CodeCrafters Challenge

Resources

Stars

1 star

Watchers

0 watching

Forks

Contributors

Languages