A minimalist yet powerful shell implementation - as beautiful as a shell
Minishell is a custom shell implementation written in C that mimics the core functionality of bash. This project demonstrates deep understanding of system programming concepts including process management, file descriptors, signal handling, and command parsing. Built with elegance and efficiency in mind, Minishell provides a robust command-line interface that handles complex scenarios with the grace of a seasoned shell.
- Interactive Prompt: Displays a clean prompt waiting for user commands
- Command History: Maintains a working history of executed commands
- Executable Search: Locates and launches programs using PATH variable or absolute/relative paths
- Signal Management: Proper handling of shell signals with bash-like behavior
- Memory Safety: Zero memory leaks with comprehensive cleanup
- Quote Handling: Sophisticated parsing of single and double quotes
- Variable Expansion: Environment variable substitution with
$VARsyntax - Exit Status Tracking: Access last command's exit code via
$? - Pipeline Support: Command chaining with pipes (
|) - I/O Redirection: Complete redirection suite (
<,>,>>,<<)
A comprehensive set of essential shell built-ins, each implemented to match bash behavior:
echo(with-noption support)cd(relative and absolute path navigation)pwd(current directory display)export(environment variable management)unset(variable removal)env(environment display)exit(graceful shell termination)
- GCC compiler
- Make utility
- Standard C library
- Readline library
# Clone the repository
git clone [email protected]:Melovii/minishell.git
cd minishell
# Compile the project
makeThe Makefile includes all standard rules and compiles with strict flags (-Wall -Wextra -Werror) ensuring code quality.
./minishellminishell> echo "Hello, World!"
Hello, World!
minishell> ls -la | grep minishell
-rwxr-xr-x 1 user staff 12345 Jan 01 12:00 minishell
minishell> export MY_VAR="test value"
minishell> echo $MY_VAR
test value
minishell> pwd
/path/to/current/directory| Command | Syntax | Description |
|---|---|---|
echo |
echo [-n] [text...] |
Display text with optional newline suppression |
cd |
cd [path] |
Change current directory |
pwd |
pwd |
Print current working directory |
export |
export [VAR=value] |
Set environment variables |
unset |
unset [VAR] |
Remove environment variables |
env |
env |
Display all environment variables |
exit |
exit [status] |
Exit shell with optional status code |
Minishell processes commands through a sophisticated multi-stage pipeline:
- Tokenization: Input string is broken into meaningful tokens
- Parsing: Tokens are organized into command structures with proper syntax validation
- Expansion: Environment variables and special parameters are expanded
- Execution: Commands are executed with proper process management and I/O handling
This design ensures robust command processing while maintaining excellent performance.
command < file- Redirect input from filecommand > file- Redirect output to file (overwrite)command >> file- Redirect output to file (append)command << delimiter- Here-document input until delimiter
cmd1 | cmd2 | cmd3- Chain commands with seamless data flow- Supports complex multi-stage pipelines with proper error propagation
Minishell implements bash-compatible signal behavior:
- Ctrl+C: Interrupts current command and displays new prompt
- Ctrl+D: Gracefully exits the shell (EOF)
- Ctrl+\: Ignored (no action taken)
Signal handling uses minimal global state (single global variable) for clean implementation.
$VAR- Expands to the value of environment variable VAR$?- Expands to exit status of last executed command- Handles unset variables gracefully
A sophisticated quoting system that perfectly matches bash behavior:
# Set USER environment variable for testing
minishell> export USER="john_doe"
# Test single quotes within double quotes
minishell> echo "'$USER'"
'john_doe'
# Test double quotes within single quotes
minishell> echo '"$USER"'
"$USER"This nuanced handling demonstrates the shell's advanced parsing capabilities!
- Preserve literal value of all characters within quotes
- No variable expansion or special character interpretation
- Cannot contain single quotes
- Allow variable expansion with
$ - Preserve literal value of most characters
- Enable complex string construction with embedded variables
Minishell is built for robustness, gracefully handling:
- Input Validation: Empty commands, malformed syntax, unclosed quotes
- File Operations: Permission errors, non-existent files, invalid paths
- Variable Handling: Unset variables, complex expansion scenarios
- Memory Management: Comprehensive leak prevention and cleanup
- Process Control: Proper signal handling and child process management
- Special Characters: Filenames with spaces, complex quoting scenarios
- Pipeline Edge Cases: Empty commands in pipelines, multiple redirections
The shell includes sophisticated quote handling that perfectly replicates bash behavior, including the complex interaction between single and double quotes in nested scenarios.
- Does not support command line editing features (arrow keys for history navigation)
- Backslash escaping is not implemented
- Advanced bash features like command substitution are not supported
minishell/
βββ includes/ # Header files
βββ libft/ # Custom C library
βββ srcs/ # Source code
β βββ builtins/ # Built-in command implementations
β βββ execution/ # Command execution and process management
β βββ parsing/ # Tokenization and command parsing
β βββ utils/ # Utility functions
β βββ main.c # Entry point
βββ Makefile # Build configuration
Created as part of the 42 School curriculum - a testament to systems programming mastery.
"The shell is not just a program, but a philosophy of elegant simplicity."
This project is open source and available under the MIT License.