Skip to content

AhmedHodiani/minishell

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Minishell

A Unix shell implementation in C, built as part of the 42 curriculum.

Overview

This project implements a basic Unix shell with support for command execution, pipes, redirections, environment variable expansion, and built-in commands.

Custom Dependencies

Rather than relying on external libraries, I built three custom dependencies from scratch:

libft

A custom C standard library containing reimplementations of common functions:

  • String manipulation (strlen, strcpy, strjoin, split, etc.)
  • Memory management (memcpy, memset, calloc, etc.)
  • Character classification (isalpha, isdigit, etc.)
  • Linked list operations
  • Custom printf implementation
  • Get next line for reading input

libgc

A garbage collector library that handles automatic memory management:

  • Memory allocation tracking
  • Automatic cleanup on exit
  • Prevents memory leaks by maintaining a collection of allocated pointers
  • Custom malloc/free wrappers

libtrue

A data structures library providing:

  • Generic linked list implementation
  • Function pointers for flexible data handling
  • Iterator and mapper functions

Core Concepts Learned

Process Management

  • Fork and exec system calls
  • Parent-child process relationships
  • Process synchronization with wait/waitpid
  • Pipe communication between processes

File Descriptors

  • Standard input/output/error (0, 1, 2)
  • File descriptor duplication with dup2
  • Pipe file descriptors for inter-process communication
  • Handling multiple redirections

Parsing and Tokenization

  • Lexical analysis of command-line input
  • Handling quotes (single and double)
  • Environment variable expansion
  • Heredoc implementation

Signal Handling

  • Catching and handling SIGINT (Ctrl+C)
  • SIGQUIT (Ctrl+) behavior
  • Different signal behavior in parent vs child processes

Memory Management

  • Tracking all allocations
  • Avoiding memory leaks in complex parsing scenarios
  • Building a garbage collector to simplify cleanup

Building

make

Usage

./minishell

The shell supports:

  • Command execution from PATH
  • Pipes (|)
  • Redirections (<, >, <<, >>)
  • Environment variables
  • Built-ins: echo, cd, pwd, export, unset, env, exit

Built-in Commands

  • echo: Print arguments to stdout (supports -n flag)
  • cd: Change directory
  • pwd: Print working directory
  • export: Set environment variables
  • unset: Unset environment variables
  • env: Print environment variables
  • exit: Exit the shell

Project Structure

.
├── main.c                  # Entry point and main loop
├── include/                # Header files
├── sources/                # Core functionality
│   ├── buildin/           # Built-in command implementations
│   └── utils/             # Helper functions
└── dependencies/           # Custom libraries
    ├── libft/             # Standard library reimplementation
    ├── libgc/             # Garbage collector
    └── libtrue/           # Data structures

About

Minishell – A Bash-like shell in C with pipes, redirections, built-ins, and signal handling.

Resources

Stars

Watchers

Forks

Contributors 2

  •  
  •