Skip to content

AimonKied/42-minishell

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

178 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

🐚 Minishell

Language Grade School

As beautiful as a shell β€” A fully functional UNIX command-line interpreter built from scratch in C

πŸ“‹ Overview

Minishell is a project from the 42 School curriculum that challenges students to recreate a simplified version of Bash. This project demonstrates deep understanding of process management, file descriptors, signal handling, and parsing techniques β€” all implemented in pure C following strict coding standards.

This shell implementation handles real-world command-line scenarios including piping, redirections, environment variables, and built-in commands, providing a fully interactive shell experience.

✨ Features

🎯 Core Shell Functionality

  • Interactive & Non-Interactive Modes β€” Full support for both terminal and scripted usage
  • Command History β€” Navigate through previous commands using arrow keys (via GNU Readline)
  • Signal Handling β€” Proper handling of Ctrl+C, Ctrl+D, and Ctrl+\ signals

πŸ”§ Built-in Commands

Command Description
echo Display text with -n flag support
cd Change directory with relative/absolute paths
pwd Print current working directory
export Set environment variables
unset Remove environment variables
env Display all environment variables
exit Exit the shell with optional exit code

πŸ”€ Redirections & Pipes

  • Input Redirection (<) β€” Read input from a file
  • Output Redirection (>) β€” Write output to a file
  • Append Mode (>>) β€” Append output to a file
  • Here Document (<<) β€” Read input until a delimiter is reached
  • Pipes (|) β€” Chain multiple commands together

πŸ”€ Advanced Features

  • Environment Variable Expansion β€” $VAR and $? (exit status)
  • Quote Handling β€” Single (') and double (") quotes with proper escaping
  • Tilde Expansion β€” ~ expands to home directory
  • Path Resolution β€” Commands searched in $PATH

πŸ—οΈ Architecture

The project follows a modular architecture with clear separation of concerns:

β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚                         MINISHELL                            β”‚
β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
β”‚                                                              β”‚
β”‚  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”    β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”    β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”    β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β” β”‚
β”‚  β”‚  LEXER   β”‚ -> β”‚  PARSER  β”‚ -> β”‚ EXECUTOR β”‚ -> β”‚ OUTPUT β”‚ β”‚
β”‚  β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜    β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜    β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜    β””β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β”‚
β”‚       β”‚              β”‚               β”‚                       β”‚
β”‚       β–Ό              β–Ό               β–Ό                       β”‚
β”‚  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”    β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”    β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”               β”‚
β”‚  β”‚  Tokens  β”‚    β”‚  AST/    β”‚    β”‚ Process  β”‚               β”‚
β”‚  β”‚  Stream  β”‚    β”‚  Cmds    β”‚    β”‚ Mgmt     β”‚               β”‚
β”‚  β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜    β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜    β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜               β”‚
β”‚                                                              β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

πŸ“ Project Structure

minishell/
β”œβ”€β”€ include/              # Header files
β”‚   β”œβ”€β”€ minishell.h       # Main data structures
β”‚   β”œβ”€β”€ lexer.h           # Tokenization definitions
β”‚   β”œβ”€β”€ parser.h          # Parsing functions
β”‚   β”œβ”€β”€ execute.h         # Execution & builtins
β”‚   β”œβ”€β”€ garbage_collector.h
β”‚   └── libft/            # Custom C library
β”‚
β”œβ”€β”€ src/                  # Source files
β”‚   β”œβ”€β”€ main.c            # Entry point
β”‚   β”œβ”€β”€ shell.c           # Shell loop & initialization
β”‚   β”‚
β”‚   β”œβ”€β”€ lexer*.c          # Tokenization (7 files)
β”‚   β”œβ”€β”€ parser*.c         # Command parsing (4 files)
β”‚   β”œβ”€β”€ execute*.c        # Command execution (2 files)
β”‚   β”œβ”€β”€ pipes*.c          # Pipeline handling (2 files)
β”‚   β”œβ”€β”€ redirect.c        # I/O redirections
β”‚   β”œβ”€β”€ heredoc*.c        # Here document handling (7 files)
β”‚   β”‚
β”‚   β”œβ”€β”€ builtin.c         # Built-in dispatcher
β”‚   β”œβ”€β”€ echo.c            # echo implementation
β”‚   β”œβ”€β”€ cd.c              # cd implementation
β”‚   β”œβ”€β”€ pwd.c             # pwd implementation
β”‚   β”œβ”€β”€ env.c             # env implementation
β”‚   β”œβ”€β”€ export*.c         # export implementation (3 files)
β”‚   β”œβ”€β”€ unset.c           # unset implementation
β”‚   β”œβ”€β”€ exit.c            # exit implementation
β”‚   β”‚
β”‚   β”œβ”€β”€ env_*.c           # Environment management
β”‚   β”œβ”€β”€ garbage*.c        # Memory management
β”‚   └── signals.c         # Signal handling
β”‚
└── Makefile              # Build configuration

πŸ”§ How It Works

1️⃣ Lexical Analysis (Tokenization)

The lexer breaks raw input into meaningful tokens:

Input:  echo "Hello World" | grep Hello > output.txt
Tokens: [echo] ["Hello World"] [|] [grep] [Hello] [>] [output.txt]

2️⃣ Parsing

The parser constructs a command list with proper structure:

  • Commands are organized into nodes
  • Redirections are attached to their respective commands
  • Pipes create linked command chains

3️⃣ Execution

The executor processes the command structure:

  • Single builtin: Executed in the main process (preserves environment changes)
  • External commands: Forked into child processes
  • Pipelines: Multiple processes connected via pipe file descriptors

4️⃣ Memory Management

A custom garbage collector tracks all allocations, ensuring clean memory handling throughout the shell's lifecycle.

πŸš€ Getting Started

Prerequisites

  • GCC or Clang compiler
  • GNU Readline library
  • Make build system
  • Linux/macOS operating system

Installation

# Clone the repository
git clone https://github.com/AimonKied/42-minishell.git

# Navigate to project directory
cd 42-minishell

# Compile the project
make

# Run minishell
./minishell

Compilation Flags

The project compiles with strict flags to ensure code quality:

-Wall -Wextra -Werror

πŸ’» Usage Examples

# Start the shell
./minishell

# Basic command execution
msh-1.0$ ls -la

# Pipeline example
msh-1.0$ cat file.txt | grep "pattern" | wc -l

# Redirections
msh-1.0$ echo "Hello" > output.txt
msh-1.0$ cat < input.txt >> output.txt

# Here document
msh-1.0$ cat << EOF
> Line 1
> Line 2
> EOF

# Environment variables
msh-1.0$ export MY_VAR="Hello World"
msh-1.0$ echo $MY_VAR

# Exit with status code
msh-1.0$ exit 42

πŸ› οΈ Technical Highlights

System Calls Used

System Call Purpose
fork() Create child processes
execve() Execute external programs
pipe() Create inter-process communication
dup2() Redirect file descriptors
wait()/waitpid() Synchronize parent/child processes
signal() Handle system signals
tcsetpgrp() Terminal process group management

Key Data Structures

  • Token List β€” Linked list of lexical tokens
  • Command List β€” Linked list of command nodes with associated files
  • Environment List β€” Linked list managing environment variables
  • Heredoc List β€” Linked list for here document content

πŸ“Š Skills Demonstrated

Skill Application
Process Management Fork, exec, wait, pipes
Memory Management Custom garbage collector, leak-free code
Signal Handling SIGINT, SIGQUIT, proper terminal control
File I/O Redirections, here documents
Parsing Lexical analysis, tokenization, syntax validation
Data Structures Linked lists, dynamic arrays
System Programming POSIX compliance, Unix internals

πŸ‘₯ Team

This project was developed as a collaborative effort at 42 Heilbronn:

  • swied β€” Shell core, execution, builtins, heredocs
  • vramacha β€” Lexer, parser, expansion, signals

πŸ“œ License

This project is part of the 42 School curriculum. Feel free to explore and learn from the code!


Built with ❀️ at 42 Heilbronn

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors