A compact recreation of the bash shell, implementing key concepts such as file descriptors, process management, and command parsing. This 42 School project demonstrates systems programming fundamentals through building a functional UNIX shell from scratch.
Minishell is a simplified implementation of bash that handles essential shell operations including command execution, pipes, redirections, and built-in commands. Built entirely in C with strict memory management and following the 42 coding standards.
- Interactive Prompt: Command-line interface with history support
- Command Execution: System executables and local programs
- Built-in Commands: Essential shell commands implemented natively
- Pipes & Redirections: Full I/O redirection and command chaining
- Environment Variables: Variable expansion and manipulation
- Signal Handling: Proper Ctrl+C, Ctrl+D, and Ctrl+\ behavior
- Quote Parsing: Single and double quote handling
- Error Management: Comprehensive error handling and exit codes
| Command | Description | Options |
|---------|------------------------------|-------------------------|
| `echo` | Display text | `-n` (no newline) |
| `cd` | Change directory | Relative/absolute paths |
| `pwd` | Print working directory | None |
| `export`| Set environment variables | Variable assignment |
| `unset` | Remove environment variables | Variable names |
| `env` | Display environment | None |
| `exit` | Exit shell | Optional exit code |
| Oper | Function | Example |
|------|------------------------------|------------------------------------|
| `|` | Pipe output to next command | `ls | grep .c` |
| `>` | Redirect output | `echo "text" > file.txt` |
| `>>` | Append output | `date >> log.txt` |
| `<` | Redirect input | `sort < data.txt ` |
| `<<` | Here document | `cat << EOF` |
| `&&` | Execute if previous succeeds | `make && ./program` |
| `||` | Execute if previous fails | `test -f file || echo "not found"` |
| `&` | Run command in background | `sleep 10 &` |
minishell$ cat processed.txt
minishell$ ls >> file_list.txt
- Variable Expansion:
$HOME,$USER,$PATH - Exit Status:
$?expands to last command's exit code - Custom Variables: Set and use custom environment variables
- Quote Handling: Different expansion behavior in single vs double quotes
minishell$ export MY_VAR="test value"
minishell$ echo $MY_VAR
test value
minishell$ echo "Exit code: $?"
Exit code: 0
- Ctrl+C (SIGINT):
- If foreground process running: send SIGINT to child
- If no foreground process: display new prompt
- Ctrl+D: Exit minishell cleanly
- Ctrl+Z (SIGTSTP): Stop foreground process and show PID
- **Ctrl+**: No action (ignored)
minishell$ cat This is a here document
> Multiple lines supported
> EOF
This is a here document
Multiple lines supported
minishell/
βββ src/ # Source files
β βββ main.c # Program entry point
β βββ parser/ # Command parsing logic
β β βββ lexer.c # Tokenization
β β βββ parser.c # Syntax analysis
β β βββ expander.c # Variable expansion
β βββ executor/ # Command execution
β β βββ executor.c # Main execution logic
β β βββ pipes.c # Pipe handling
β β βββ redirections.c # I/O redirection
β βββ builtins/ # Built-in commands
β β βββ echo.c # Echo implementation
β β βββ cd.c # Directory change
β β βββ pwd.c # Working directory
β β βββ export.c # Environment export
β β βββ unset.c # Variable removal
β β βββ env.c # Environment display
β β βββ exit.c # Shell exit
β βββ signals/ # Signal handling
β βββ utils/ # Utility functions
βββ includes/
β βββ minishell.h # Header file
βββ Makefile # Build configuration
βββ README.md
Lexical Analysis: Tokenizes input into meaningful components (commands, operators, arguments)
Syntax Parsing: Builds command structures and validates syntax
Variable Expansion: Processes environment variables and special parameters
Process Management: Handles fork/exec for command execution
File Descriptor Management: Manages pipes and redirections efficiently
Memory Management: Ensures zero memory leaks with proper cleanup
Command Pipeline: Implements pipe chains with proper file descriptor handling
Quote Processing: Handles nested quotes and escape sequences
Path Resolution: Searches PATH environment variable for executables
Signal Safety: Implements async-signal-safe signal handlers
# Test basic commands
./minishell
minishell$ echo test
minishell$ pwd
minishell$ cd ..
# Test pipes and redirections
minishell$ ls | wc -l
minishell$ echo "test" > file.txt
minishell$ cat < file.txt
# Test environment variables
minishell$ export TEST=value
minishell$ echo $TEST
minishell$ unset TEST
- Empty commands and multiple spaces
- Unclosed quotes and syntax errors
- Invalid file permissions
- Signal interruption during execution
- Large command pipelines
Minishell does not support:
- Wildcards (
*,?,[]) - Command substitution (
`command`or$(command)) - Logical operators (
&&,||) - Command separators (
;) - Backslash escaping (
\) - Job control and background processes (
&)
This is a 42 School project, but contributions for educational purposes are welcome:
- Fork the repository
- Create a feature branch (
git checkout -b feature/improvement) - Commit your changes (
git commit -m 'Add improvement') - Push to the branch (
git push origin feature/improvement) - Open a Pull Request
- GNU Bash Manual
- POSIX Shell Specification
- Advanced Programming in UNIX Environment
- Systems Programming Book
- Norm Compliance: Follows 42 coding standards
- Memory Management: Zero memory leaks (verified with Valgrind)
- Error Handling: Comprehensive error management
- Function Restrictions: Limited to approved system calls
- Global Variables: Minimal global state usage
This project is part of the 42 School curriculum. Use for educational purposes only.
Maksym Voloshyn
- GitHub: @voloshynm
- 42 Login: mvoloshyn
- Email: mvoloshy@student.42luxembourg.lu
Sergio Filipe
- GitHub: @
- 42 Login: sandre-a
- Email: sandre-a@student.42luxembourg.lu
- 42 School - Project specification and learning framework
- GNU Bash Team - Reference implementation and documentation
- UNIX Pioneers - Shell design principles and philosophy
- Fellow 42 Students - Collaboration and peer learning
"Understanding shells means understanding UNIX" - Building minishell teaches the fundamentals of systems programming
Keywords: c-programming unix-shell 42-school systems-programming bash shell pipes redirections process-management