A POSIX-compliant shell implementation in Rust, built for the CodeCrafters "Build Your Own Shell" Challenge.
π Special thanks to CodeCrafters for providing the excellent learning platform and structured challenges that made building this shell implementation possible. The CodeCrafters team has created an outstanding educational experience that guides developers through building complex systems from scratch.
- REPL Interface - Interactive shell with command prompt and history
- Command Parsing - Robust parsing of shell commands with arguments
- Process Execution - Run external programs and manage child processes
- Built-in Commands - Implement common shell utilities
cd <path>- Change working directory with path resolutionpwd- Print current working directoryecho [args...]- Display messages with argument expansion supporttype <command>- Show command type information (built-in vs external)history- Display command history with optional limits and file operationslocate <command>- Find executable files in PATHexit- Exit the shell
- Command Pipelines - Full support for command piping (
|) with multi-stage pipelines - I/O Redirection - Complete redirection support:
>- Overwrite output to file>>- Append output to file<- Input from file- Error stream redirection (
2>,2>>)
- Auto-completion - Tab completion for:
- Built-in commands
- External executables in PATH
- File and directory paths
- History Management - Thread-safe history with:
- In-memory storage
- Navigation via arrow keys
- History search functionality
- Path Helper - Utilities for path manipulation and resolution
- External Command Execution - Run any system command with proper process management
The shell is organized into several well-structured modules:
src/
βββ main.rs # Application entry point and initialization
βββ lib.rs # Library crate root
βββ core/ # Core utilities and shared functionality
β βββ mod.rs # Core module definitions
β βββ utils.rs # Shared utility functions
βββ shell/ # Main shell engine and REPL loop
β βββ mod.rs # Shell module definitions
β βββ engine.rs # Shell engine with REPL implementation
βββ parsing/ # Command parsing and tokenization
β βββ mod.rs # Parsing module definitions
β βββ command_parser.rs # Command line parsing logic
βββ commands/ # Command system architecture
β βββ mod.rs # Command module root
β βββ core/ # Command infrastructure
β β βββ mod.rs # Core command definitions
β β βββ command_handler.rs # Command handler trait
β β βββ registry.rs # Command registration system
β β βββ history_state.rs # History state management
β β βββ supported_command.rs # Command enumeration and types
β βββ handlers/ # Individual command implementations
β β βββ mod.rs # Handler module definitions
β β βββ cd_command_handler.rs # Change directory command
β β βββ echo_command_handler.rs # Echo command implementation
β β βββ history_command_handler.rs # History command handling
β β βββ locate_command_handler.rs # Command location finder
β β βββ pipeline_command_handler.rs # Pipeline command processing
β β βββ pwd_command_handler.rs # Print working directory
β β βββ redirection_command_handler.rs # I/O redirection handling
β β βββ type_command_handler.rs # Command type checker
β β βββ unspecified_command_handler.rs # External command execution
β βββ utils/ # Command utilities and helpers
β βββ mod.rs # Utils module definitions
β βββ path_helper.rs # Path manipulation utilities
βββ auto_complete/ # Tab completion functionality
βββ mod.rs # Auto-completion module
βββ auto_complete_helper.rs # Tab completion implementation
- Shell Engine (
src/shell/engine.rs) - Main REPL loop with rustyline integration - Command Registry (
src/commands/core/registry.rs) - Extensible command registration system - Command Parser (
src/parsing/command_parser.rs) - Robust command line parsing - Handler System (
src/commands/handlers/) - Modular command implementations - History Management (
src/commands/core/history_state.rs) - Thread-safe command history - Auto-completion (
src/auto_complete/) - Tab completion for commands and paths
# Run the shell
./your_program.sh
# Or using cargo
cargo runThe shell provides an interactive prompt where you can type commands:
$ pwd
/home/user/project
$ ls -la
$ echo "Hello, World!"
$ cd /tmp
$ history# Basic commands
$ echo "Hello from Rust Shell"
$ pwd
/home/user/project
# Directory navigation
$ cd /home
$ pwd
/home
# Redirection
$ echo "test" > output.txt
$ cat < input.txt
# Pipelines
$ ls -la | grep ".rs"
$ echo "hello" | wc -c
# Auto-completion (press Tab)
$ cd /ho<Tab> # Completes to /home/
$ echo <Tab> # Shows available files- Rust 1.92 or later
- Cargo package manager
# Build the project
cargo build
# Run tests
cargo test
# Check formatting
cargo fmt --check
# Run linter
cargo clippy
# Build in release mode
cargo build --release- rustyline - Advanced readline functionality with history and auto-completion
- anyhow - Error handling and result management
- regex - Regular expression support for parsing
- Command Pattern - Extensible command handler system
- Registry Pattern - Dynamic command registration and discovery
- Thread Safety - Arc<Mutex<>> for shared state management
- Trait-based Design - Modular and testable command handlers
- Zero-copy Parsing - Efficient string handling for command parsing
- Lazy Evaluation - On-demand command execution and validation
- Memory Efficient - Minimal allocations in hot paths
This project is part of the CodeCrafters "Build Your Own Shell" challenge. The implementation follows CodeCrafters' structured learning approach and testing methodology.
- Make changes to your implementation
- Commit and push to GitHub:
git commit -am "update implementation" git push origin master - Check the CodeCrafters dashboard for automated test results
- Incremental Development - Features built step-by-step with validated checkpoints
- Test-Driven Learning - Each stage includes comprehensive test coverage
- Best Practices - Code quality, error handling, and performance optimization
- Community Support - Access to expert guidance and peer discussions
This implementation demonstrates mastery of systems programming concepts including process management, parsing algorithms, concurrent programming, and Unix/Linux system calls - all taught through CodeCrafters' excellent curriculum.
Feel free to submit issues and enhancement requests!
This project is open source and available under the MIT License.