Minishell
3 min readJul 13, 2025
42 Minishell Project Guide
Table of Contents
Project Overview
Minishell is a 42 project that requires you to create a simplified version of bash shell. The goal is to understand how shells work, process management, and system calls.
Learning Objectives
- Understand shell mechanics and command execution
- Learn about process creation and management
- Master file descriptor manipulation
- Implement signal handling
- Work with environment variables
- Parse and interpret command line input
Mandatory Requirements
General Rules
- Write in C language
- Follow the Norm (42 coding standard)
- No memory leaks, segfaults, or undefined behavior
- Create a Makefile with rules:
all,clean,fclean,re - Handle errors gracefully without crashing
- Use only allowed functions (see allowed functions list)
Allowed Functions
readline, rl_clear_history, rl_on_new_line, rl_replace_line, rl_redisplay
add_history, printf, malloc, free, write, access, open, read, close
fork, wait, waitpid, wait3, wait4, signal, sigaction, sigemptyset
sigaddset, kill, exit, getcwd, chdir, stat, lstat, fstat, unlink
execve, dup, dup2, pipe, opendir, readdir, closedir, strerror, perror
isatty, ttyname, ttyslot, ioctl, getenv, tcsetattr, tcgetattr
tgetent, tgetflag, tgetnum, tgetstr, tgoto, tputsTechnical Specifications
Core Features
- Display a prompt when waiting for new commands
- Command history functionality
- Search and launch executables (relative/absolute path or using PATH)
- Handle single quotes (
') - prevent interpretation of metacharacters - Handle double quotes (
") - prevent interpretation except for$ - Implement redirections:
<redirect input>redirect output<<heredoc>>append output
- Implement pipes (
|) - Handle environment variables (
$followed by characters) - Handle
$?(exit status of last executed command) - Handle
ctrl-C,ctrl-D,ctrl-\signals - Implement built-in commands
Program Structure
- Program name:
minishell - Handle one or more options/arguments (if needed)
- Interactive mode: display prompt and wait for commands
- Non-interactive mode: not required but can be implemented
Built-in Commands
1. echo with option -n
- Print arguments to stdout
-noption: don't print trailing newline- Handle multiple arguments and spaces
2. cd with relative or absolute path
- Change current directory
- Handle
~(home directory) - Handle
-(previous directory) - Handle no arguments (go to HOME)
- Update PWD and OLDPWD environment variables
3. pwd with no options
- Print current working directory
- Should work even if PWD is unset or modified
4. export with no options
- Set environment variables
- Display all exported variables when no arguments
- Handle
var=valueformat - Handle invalid variable names
Get Crouns’s stories in your inbox
Join Medium for free to get updates from this writer.
5. unset with no options
- Remove environment variables
- Handle multiple variables
- Don’t unset read-only variables
6. env with no options or arguments
- Display all environment variables
- Each variable on a new line in
key=valueformat
7. exit with no options
- Exit the shell
- Handle numeric arguments
- Handle multiple arguments (error case)
- Exit with appropriate status code
Signal Handling
Interactive Mode
ctrl-C: Display new prompt on new linectrl-D: Exit shellctrl-\: Do nothing (ignore)
Non-Interactive Mode (commands running)
ctrl-C: Stop current command, display new promptctrl-D: EOF behaviorctrl-\: Quit current command
Implementation Notes
- Use
sigactionorsignalfunctions - Handle signals appropriately in parent and child processes
- Don’t let signals kill the shell unexpectedly
Environment Variables
Expansion Rules
$VARor${VAR}: expand to value of VAR$?: expand to exit status of last command$$: expand to process ID (optional)- Variables in double quotes are expanded
- Variables in single quotes are NOT expanded
Special Cases
- Undefined variables expand to empty string
- Handle
$followed by invalid characters - Handle
$at end of string
Parsing and Execution
Command Parsing
- Tokenization: Split input into tokens
- Quote handling: Process single and double quotes
- Variable expansion: Expand
$variables - Redirection parsing: Identify and parse
<,>,<<,>> - Pipeline parsing: Split on
|operators
Execution Order
- Parse command line
- Handle redirections
- Check for built-in commands
- Search for external commands
- Execute command
- Handle return status
Edge Cases
- Empty commands
- Commands with only spaces
- Multiple pipes
- Redirections with pipes
- Invalid file permissions
- Non-existent commands
Error Handling
Common Errors
- Command not found: Display error message
- Permission denied: Handle file permissions
- No such file or directory: Handle missing files
- Syntax errors: Handle malformed commands
- Memory allocation failures: Handle malloc failures
Error Messages
- Should be similar to bash error messages
- Use
perrororstrerrorfor system errors - Display errors to stderr
- Set appropriate exit status





