Skip to content

UmarlyPoeta/shell-cpp

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

7 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

shell-cpp

A minimal interactive Unix-like shell written in C++23.

It supports:

  • interactive prompt (GNU readline)
  • command history (saved to a file)
  • basic builtins
  • running external commands via execvp
  • pipelines (cmd1 | cmd2 | ...)
  • stdout/stderr redirection (>, >>, 2>, 2>>, 1>, 1>>)
  • tab completion for builtins and executables found in $PATH

Repo: https://github.com/UmarlyPoeta/shell-cpp


Requirements

  • Linux / macOS (POSIX: fork, execvp, waitpid, pipe)
  • CMake >= 3.13
  • A C++ compiler with C++23 support
  • readline development package

On Debian/Ubuntu:

sudo apt-get update
sudo apt-get install -y build-essential cmake libreadline-dev

On Fedora:

sudo dnf install -y gcc-c++ cmake readline-devel

Building

cmake -S . -B build
cmake --build build -j

The binary will be:

  • build/shell

Running

./build/shell

You should see a banner and a prompt:

patryk-shell v0.1 $

Builtins

Implemented builtins (see src/builtins.cpp):

  • echo ... – prints arguments
  • pwd – prints current directory
  • cd <dir> – changes directory (supports ~ expansion)
  • type <cmd> – prints whether command is a builtin or a path-resolved executable
  • history – prints history
    • history -r <file> – read history from file
    • history -w <file> – write history to file
    • history -c – clear in-memory history
    • history -a <file> – append new entries since last save
  • exit – exits the shell (and saves history)

Pipelines

Pipelines are supported using |:

ls -la | grep cpp | wc -l

Redirection

Supported operators:

  • > / 1> – redirect stdout (truncate)
  • >> / 1>> – redirect stdout (append)
  • 2> – redirect stderr (truncate)
  • 2>> – redirect stderr (append)

Examples:

echo hello > out.txt
ls does-not-exist 2> err.txt
ls -la >> out.txt

History file

History is stored in:

  • $HISTFILE if set
  • otherwise .my_shell_history in the current working directory

Code structure

Key files:

  • src/main.cpp – REPL loop + dispatch (builtins / external / pipelines)
  • src/parser.cpp – parsing input / redirections / pipelines
  • src/executor.cpp – running external commands and pipelines
  • src/builtins.cpp – builtin implementations
  • src/completions.cpp – readline completion
  • src/utils.cpp – helper functions + banner + history helpers

Notes / Limitations

This is a learning project and intentionally minimal.

Not implemented (yet):

  • job control (fg, bg, signals)
  • environment variable expansion ($VAR)
  • globbing (*.cpp)
  • advanced quoting/escaping edge cases
  • background execution (&)

License

MIT (or specify your preferred license). If you want, add a LICENSE file.

About

This project is a custom shell built from scratch using C++. It provides a command-line interface that supports executing external programs and several built-in commands. Copied from my project on codecrafters

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors