An enhanced C++ implementation of the "Build Your Own Shell" challenge from CodeCrafters. This repository contains a small interactive shell (REPL) that supports builtin commands, executing external programs, basic redirections, and cross-platform process launching on both POSIX and Windows.
This README documents how to build and run the project, shows usage examples and explains a number of extensions implemented beyond the minimal starter requirements.
- Cross-platform: builds and runs on POSIX (Linux/WSL/macOS) and Windows.
- Builtin commands:
echo,cd(supports~),pwd,type,exit. - External program execution (resolves PATH entries).
- Redirections: supports
>,>>and numeric fds like2>err.txt. - Robust argument parsing: single/double quotes, backslash escapes inside and outside quotes, and error on unclosed quotes.
- Windows-specific support:
.exe/.bat/.cmdresolution, CreateProcessW based spawning, and careful argument quoting and UTF-8 -> UTF-16 conversion. - Scoped redirections for builtins (so builtins honor redirected stdout/stderr).
cdresolves and canonicalizes the target directory and supports~.
The implementation files of note are in src/:
src/main.cpp— program entrypoint and minimal runner.src/shell.h,src/shell.cpp— shell class, parsing, dispatching, PATH resolution and builtin implementations.src/utils.h— process launching (POSIX fork/exec and Windows CreateProcess), argument parser, home directory lookup, redirection helpers, etc.
If you or someone on your team extended the starter, the most notable additions in this codebase are:
- Full cross-platform process launching: CreateProcessW for Windows with
careful quoting and UTF-8 to UTF-16 conversion. POSIX uses
fork+execv. - Argument parser that closely mirrors shell-like quoting behavior (single quotes, double quotes with limited escaping, and backslash escapes).
- Redirection parsing and handling that supports numeric fds and append
(
>>) and aScopedRedirhelper that temporarily redirects file descriptors for builtin commands. - PATH-resolution that tries platform-appropriate executable extensions on Windows (.exe, .bat, .cmd) and checks executable bit on POSIX.
- Improved
cdhandling: supports~and canonicalizes the working directory (resolving symlinks where possible).
These features make the shell more robust and suitable for manual testing on both Windows and POSIX environments.
- C++17 compatible compiler (MSVC, g++, clang++)
- CMake (3.10+ recommended)
- On Windows: Visual Studio or Build Tools (so CMake can generate a Visual Studio solution or Ninja files). On Linux/WSL: make/ninja + standard build toolchain.
The repository contains a CMake-based build. Example commands below assume you run them from the repository root.
Windows (recommended via Developer Command Prompt or PowerShell):
cmake -S . -B build -DCMAKE_BUILD_TYPE=Release
cmake --build build --config Release
# Executable will usually be at build\Release\shell.exe (or the configured target dir)
.WSL / Linux / macOS:
cmake -S . -B cmake-build-debug-wsl -DCMAKE_BUILD_TYPE=Release
cmake --build cmake-build-debug-wsl -- -j$(nproc)
# Executable will be at cmake-build-debug-wsl/shellYou can also use the provided your_program.sh wrapper if present and
appropriate for your environment (it simply runs the built executable).
Run the shell (example path depends on your build):
./cmake-build-debug-wsl/shell
# or on Windows
build\Release\shell.exeOnce inside the prompt, try examples:
-
Builtins:
-
echo:
echo hello world -
pwd:
pwd -
cd:
cd /tmporcd ~ -
type:
type lsortype echo -
exit:
exitorexit 2 -
External commands:
-
ls -la(POSIX) -
dir(Windows cmd builtin via external program resolution if available) -
Redirection examples:
-
echo hi > out.txt(overwrite) -
echo again >> out.txt(append) -
ls 2> err.txt(redirect stderr to file) -
myprog 1>out.txt 2>>err.txt(mix stdout overwrite + stderr append)
Notes:
- Numeric fd redirections are supported (e.g.
2>filetargets stderr). - Builtins use
ScopedRedirso redirection affects builtin output as expected.
- Pipes (
|), command chaining (&&,||), and background jobs (&) are not implemented in this codebase. - Shell expansions (globbing, environment variable expansion like
$HOME), input here-documents (<<), and job control are not implemented. - Quoting and escapes are generous, but there may be corner cases that differ from a full POSIX shell.
If you want, I can help implement pipes and job control next — they're the natural next features.
- Build the project (see Build instructions).
- Start the shell executable.
- Verify builtins: run
echo,pwd,cd,type,exit. - Verify external programs: run
which/typeorls/dirdepending on platform (or run a small program you compiled and put on PATH). - Verify redirections:
echo hello > /tmp/test.txt, thencat /tmp/test.txt. - On Windows, test an
.exein PATH and a.batscript;typeshould show resolved paths.
parse_argsinsrc/utils.h— argument parsing.parse_command_with_redirectinsrc/shell.cpp— buildsCommandwith argument vector and redirections parsed.run_program_and_waitinsrc/utils.h— platform-specific spawning.ScopedRedirinsrc/utils.h— temporarily redirects fds for builtins.
This solution is based on the CodeCrafters Shell challenge. Follow the original challenge terms where applicable when publishing or sharing your solution.