Hesh is a simple functional Unix shell implementation written in C++23. It provides a comprehensive command-line interface with features including command completion, history management, I/O redirection, piping, and a beautiful agnoster-inspired prompt.
- Command Execution: Full support for executing external commands from PATH
- Built-in Commands: Essential shell builtins including
cd,echo,pwd,exit,type, andhistory - Smart Command Discovery: Automatic detection and registration of executable files in PATH directories
- Interactive Mode: Real-time command input with sophisticated prompt display
- Piping Support: Full pipeline support with
|operator for chaining commands - Stream Redirection: Comprehensive redirection support:
>and>>for stdout redirection (overwrite/append)2>and2>>for stderr redirection (overwrite/append)&>and&>>for both stdout and stderr redirection
- Quote Handling: Sophisticated parsing of single quotes, double quotes, and escape sequences
- GNU Readline Integration:
- Command history with persistent storage
- Tab completion for commands
- Line editing capabilities (Emacs-style keybindings)
- History search and navigation
- Beautiful Prompt: Agnoster-inspired powerline prompt showing:
- Current username with colored background
- Current working directory (with
~abbreviation) - Visual separators using powerline fonts
- Configuration Support: Custom configuration file (
~/.heshrec) for environment variables
- Memory Safe: Modern C++ with smart pointers and RAII principles
- Modular Architecture: Clean separation of concerns with dedicated modules for:
- Command management and registration
- Token parsing and processing
- Stream redirection handling
- Readline utilities
- Extensible Design: Easy to add new built-in commands through the command interface
- C++23 compatible compiler (GCC 13+ or Clang 16+)
- CMake 3.13+
- GNU Readline library (
libreadline-devon Ubuntu/Debian) - Make for building
-
Clone the repository:
git clone https://github.com/HamzaHassanain/hesh.git cd hesh -
Build the project:
./build.sh
-
Run the shell:
./out/hesh
# Clean build
./build.sh clean
# Build and run immediately
./build.sh run
# Manual CMake build
mkdir -p out
cmake -S . -B out
cd out && make -j$(nproc)Print arguments to stdout with space separation
echo Hello World
echo "Hello World" > output.txt
echo Error message 2> error.logChange current working directory
cd /home/user
cd ~ # Go to home directory
cd .. # Go to parent directoryPrint current working directory
pwdExit the shell
exitDisplay information about command type
type echo # Shows: echo is a shell builtin
type ls # Shows: ls is /bin/lsCommand history management
history # Show all history
history 10 # Show last 10 commands
history -w file # Write history to file
history -r file # Read history from file
history -a file # Append new history to fileChain commands using the pipe operator:
echo "Hello World" | grep Hello
history | tail -5
cat file.txt | sort | uniqRedirect output streams:
echo "Hello" > output.txt # Redirect stdout to file
echo "Error" 2> error.log # Redirect stderr to file
echo "Both" &> combined.log # Redirect both streams
echo "Append" >> output.txt # Append to fileHandle complex arguments:
echo "Hello World" # Double quotes
echo 'Hello $USER' # Single quotes (literal)
echo Hello\ World # Escape spaces
echo "Path: \"$HOME\"" # Escaped quotesCreate ~/.heshrec to customize your shell:
# Set custom PATH
PATH=/custom/bin:/usr/local/bin
# Set history file location
HISTFILE=~/.my_shell_history
# Add custom environment variables
CUSTOM_VAR=valuemain.cpp: Entry point with main shell loop and readline integrationcommander.hpp: Command registry using Trie data structure for efficient lookupcmd.hpp: Base command interface with stream redirection and piping supporttokenize.hpp: Advanced tokenizer with quote handling and escape sequences
All commands inherit from the base cmd class:
echo_cmd: Text output with stream redirection supportcd_cmd: Directory navigation with~expansionpwd_cmd: Current directory displayexit_cmd: Shell terminationtype_cmd: Command type identificationhistory_cmd: History management with file operations
readline_utils.hpp: GNU Readline integration and tab completionstyle.hpp: Prompt styling and directory path formattingtoken.hpp: Token types for parsing (strings, pipes, redirections)redirect_streams.hpp: Stream redirection handling
# Debug build with all warnings
cmake -S . -B out -DCMAKE_BUILD_TYPE=Debug
cd out && make -j$(nproc)
# Release build optimized
cmake -S . -B out -DCMAKE_BUILD_TYPE=Release
cd out && make -j$(nproc)- Create a new header file in
src/cmds/ - Inherit from the
cmdbase class - Implement
custom_handle()method - Register the command in
init_shell.hpp
Example:
class my_cmd : public cmd {
public:
my_cmd() : cmd("mycommand", "description") {}
void custom_handle(std::vector<std::shared_ptr<Token>>& args,
std::string& piped_command) override {
// Implementation here
}
};The shell supports comprehensive testing of features:
# Test basic commands
echo "test" | cat
pwd && ls
cd ~ && pwd
# Test redirection
echo "output" > test.txt && cat test.txt
ls non_existent 2> error.log
# Test history
history 5- DOCUMENTATION.md - Complete technical documentation covering:
- Detailed architecture overview
- File-by-file code documentation
- API reference and design patterns
- Performance characteristics
- Extension guides for developers
- Fork the repository
- Create a feature branch (
git checkout -b feature/amazing-feature) - Make your changes following the existing code style
- Test your changes thoroughly
- Commit your changes (
git commit -m 'Add amazing feature') - Push to the branch (
git push origin feature/amazing-feature) - Open a Pull Request