A minimal shell implementation in C, created as part of the 42 curriculum. This project implements a simplified version of a Unix shell with support for command execution, pipelines, redirections, and built-in commands.
cd- Change directorypwd- Print working directoryecho- Print argumentsenv- Display environment variablesexport- Set environment variablesunset- Unset environment variablesexit- Exit the shell
- Pipelines - Chain commands using
|operator - Redirections:
- Input redirection:
< - Output redirection:
> - Append redirection:
>> - Heredoc:
<<
- Input redirection:
- Environment Variable Expansion - Support for
$VARand${VAR}syntax - Command History - Built-in history support using readline
- Signal Handling - Proper handling of SIGINT (Ctrl+C) and SIGQUIT
- External Commands - Execute system commands from PATH
gccorcccompilermakereadlinelibrary (development headers)
sudo apt-get install libreadline-devbrew install readlineTo compile the project, simply run:
makeThis will create the minishell executable.
make clean # Remove object files
make fclean # Remove object files and executable
make re # Clean and rebuildRun the shell:
./minishellYou'll see the prompt minishell$ where you can enter commands.
# Basic commands
minishell$ pwd
minishell$ ls -la
minishell$ echo "Hello, World!"
# Environment variables
minishell$ export MY_VAR="test"
minishell$ echo $MY_VAR
# Pipelines
minishell$ ls | grep .c
minishell$ cat file.txt | wc -l
# Redirections
minishell$ ls > output.txt
minishell$ cat < input.txt
minishell$ echo "text" >> file.txt
# Heredoc
minishell$ cat << EOF
> line 1
> line 2
> EOF
# Change directory
minishell$ cd /path/to/directory
minishell$ cd ~
# History
minishell$ history # Show command history
minishell$ history -c # Clear historyType exit or press Ctrl+D to exit the shell.
miniShell-2/
├── main.c # Main entry point and execution logic
├── minishell.h # Header file with all declarations
├── Makefile # Build configuration
├── exec/ # Built-in command implementations
│ ├── builtins_cd.c
│ ├── builtins_pwd.c
│ ├── builtins_echo.c
│ ├── builtins_env.c
│ ├── builtins_exit.c
│ ├── builtins_export.c
│ └── builtins_unset.c
├── parsing/ # Parsing and tokenization
│ ├── tokenization.c # Tokenize input string
│ ├── parsing.c # Parse tokens into AST
│ ├── parse_cmd.c # Parse individual commands
│ ├── split.c # String splitting utilities
│ ├── copy_env.c # Environment variable management
│ └── free_args.c # Memory cleanup
└── utils/ # Utility functions
├── utils1.c
├── utils2.c
├── export.c # Export functionality
├── unset.c # Unset functionality
├── heredoc.c # Heredoc implementation
└── helper.c # Helper functions
The shell follows a typical shell architecture:
- Read Line - Uses readline library to get user input
- Tokenization - Breaks input into tokens (commands, arguments, operators)
- Parsing - Builds an Abstract Syntax Tree (AST) from tokens
- Execution - Traverses AST and executes commands:
- Built-in commands execute directly
- External commands fork and exec
- Pipelines create pipes between processes
- Redirections modify file descriptors
- AST (Abstract Syntax Tree) - Represents the command structure
- Token - Represents a tokenized element (command, argument, operator)
- Command - Contains command name, arguments, and redirections
- Environment List - Linked list of environment variables
This is a minimal shell implementation and may not support all features of a full shell like bash:
- No support for logical operators (
&&,||) - No support for command substitution
- Limited error handling in some edge cases
- No job control (background processes,
&)
- abdelilah
- yolaidi-
This project is part of the 42 curriculum and follows the 42 coding standards.