A simple Unix-like shell written in C for learning and experimentation.
- Linux or macOS with a POSIX userland
gccorclangmake- C standard: C17; uses POSIX.1-2008 APIs
git clone https://github.com/blazejOz/mini_shell mini_shell
cd mini_shell
cmake -B build
./build/mini_shellYou don't need to compile anything to try out mini_shell. You can run the latest version directly from GitHub Packages:
docker run -it --rm ghcr.io/blazejoz/mini_shell:latestThe -it flags are required to interact with the shell's prompt. The --rm flag ensures the container is cleaned up after you exit.
mini_shell/
├── src/ # Source code files
│ ├── main.c # Entry point of the shell
│ ├── shell.c # Shell loop and command execution
│ ├── builtins.c # Implementation of built-in commands
│ ├── parser.c # Command parsing and tokenization
│ └── utils.c # Utility functions
├── include/ # Header files
│ ├── shell.h
│ ├── builtins.h
│ ├── parser.h
│ └── utils.h
├── tests/ # Test files
│ ├── test_builtins.c
│ ├── test_parser.c
├── Makefile # Build instructions
└── README.md # Project documentation
- Interactive Shell Loop: Robust REPL (Read-Eval-Print Loop) with a clean prompt.
-
Advanced Command Pipeline: Supports chaining multiple commands using the
|operator.- Efficiently handles
$N$ processes with$N-1$ concurrent pipes. - Proper file descriptor management (uses
dup2for I/O redirection between stages). - Synchronous execution: The shell waits for the entire pipeline to complete before returning to the prompt.
- Efficiently handles
-
Intelligent Command Dispatcher:
-
Built-in Commands: Executed directly in the parent process (
cd,exit,pwd,echo). -
External Binaries: Discovered via
$PATHand executed in forked subshells usingexecvp.
-
Built-in Commands: Executed directly in the parent process (
- Memory Safety: Implements a multi-level cleanup strategy to ensure no memory leaks during parsing or execution.
- Process Pipelines: Concurrent execution of piped commands.
- External Execution: Full integration with system binaries via
exec. - I/O Redirection: Implementing file descriptor hijacking for
>,>>, and<. - Signal Handling: Graceful management of
Ctrl+C(SIGINT) andCtrl+Z(SIGTSTP). - Variable Expansion: Support for
$HOME,$USER, and local shell variables. - Persistence: Command history saved to
~/.mini_shell_history.
cmake -B build
cmake --build build./build/mini_shell./build/unit_testsMIT License
This project is for educational purposes and demonstrates basic shell concepts and system programming in C.