minishell is a core project of the 42 curriculum, designed to implement a simplified Unix shell from scratch. This project involves a deep dive into process management, system calls, signal handling, and complex string parsing.
minishell is a lightweight simulation of Bash. It reads user commands, handles quotes and environment variable expansions, combines programs using pipes and redirections, and reacts to system signals (such as Ctrl-C, Ctrl-D, Ctrl-\) just like a real Bash environment.
The project demands high-quality code, strict memory management (zero leaks allowed), and a profound understanding of Unix internals like Inter-Process Communication (IPC) and File Descriptors.
- Interactive Command Handling: Support for command history using the
readlinelibrary. - Process Management: Execution of external binaries via
forkandexecveusing thePATH. - Redirections:
- Input redirection
< - Output redirection
> - Append mode redirection
>> - Heredoc
<<with support for delimiters and signal interruption.
- Input redirection
- Pipes: Multi-stage piping
|to pass data flow between commands. - Parsing Logic:
- Single quotes
' '(literal interpretation). - Double quotes
" "(supports variable expansion). - Environment variable expansion (e.g.,
$USER,$HOME,$?).
- Single quotes
- Signal Handling: Accurate simulation of Bash's behavior in interactive mode.
The following standard Bash built-ins are implemented:
echo(with-noption)cd(supports relative and absolute paths)pwdexport(manages environment variables)unsetenvexit
Clone the repository:
git clone https://github.com/Fiona-87327/42Core_minishell.git
cd 42Core_minishellUse the provided Makefile to compile:
make # Compiles the 'minishell' executable
make clean # Removes object files
make fclean # Removes object files and the executable
make re # Recompiles from scratch./minishell1. Basic Pipes and Redirection
minishell> ls -l | grep "m" > result.txt
minishell> cat < result.txt2. Environment Variable Expansion
minishell> echo "Hello $USER, status of last command: $?"3. Multi-Piping
minishell> cat Makefile | grep "CC" | wc -lThe shell processes input through a multi-stage pipeline:
- Lexer: Tokenizes the raw input string into meaningful units.
- Syntax Checker: Validates the sequence of tokens (e.g., preventing
| |). - Parser: Builds a command structure and resolves environment variables.
- Executor:
- Runs built-ins in the parent process if no pipes are present.
- Forks child processes for external commands and pipelines.
- Manages
dup2for file descriptor redirections.
- Uses a single global variable to track signals as per 42 constraints.
- Ctrl-C: Displays a new prompt on a new line.
- Ctrl-D: Quits the shell (EOF).
- **Ctrl-**: Does nothing in interactive mode to match Bash behavior.
- jiyanwang
- mhnatovs