Skip to content

Latest commit

ย 

History

63 Commits

Folders and files

NameName
Last commit message
Last commit date
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 

Repository files navigation

๐Ÿš Minishell

As beautiful as a shell.

A fully functional Unix shell written from scratch in C.
Handles pipes, redirections, environment variables, signals, and built-in commands โ€”
replicating core bash behavior with strict memory discipline.


๐Ÿ“‹ Table of Contents


๐Ÿ’ก About

Minishell is the most complex project in the 42 common core before the final exams. It requires building a bash-like shell that handles:

  • Tokenization and parsing of user input
  • Abstract Syntax Tree (AST) construction for command pipelines
  • Process creation with fork() and execve()
  • File descriptor management for redirections and pipes
  • Signal handling (Ctrl+C, Ctrl+D, Ctrl+\)
  • Environment variable expansion and management

This is a team project โ€” building it deepens your understanding of how Unix actually works under the hood.


โœจ Features

โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚                  MINISHELL FEATURES                   โ”‚
โ”œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ค
โ”‚                                                      โ”‚
โ”‚  โœ… Interactive prompt with command history           โ”‚
โ”‚  โœ… Command execution via PATH resolution             โ”‚
โ”‚  โœ… Single and double quote handling                  โ”‚
โ”‚  โœ… Environment variable expansion ($VAR, $?)         โ”‚
โ”‚  โœ… Input redirection    ( < file )                   โ”‚
โ”‚  โœ… Output redirection   ( > file )                   โ”‚
โ”‚  โœ… Append redirection   ( >> file )                  โ”‚
โ”‚  โœ… Here-document        ( << DELIMITER )             โ”‚
โ”‚  โœ… Pipes                ( cmd1 | cmd2 | cmd3 )       โ”‚
โ”‚  โœ… Exit status tracking ( $? )                       โ”‚
โ”‚  โœ… Signal handling      ( Ctrl+C, Ctrl+D, Ctrl+\ )   โ”‚
โ”‚  โœ… 7 built-in commands                               โ”‚
โ”‚                                                      โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜

๐Ÿ”ง Built-in Commands

Command Syntax Description
echo echo [-n] [text] Print text; -n suppresses trailing newline
cd cd [path] Change working directory
pwd pwd Print current working directory
export export [KEY=value] Set environment variable
unset unset [KEY] Remove environment variable
env env Display all environment variables
exit exit [status] Exit the shell with optional status code

Built-in Examples

minishell$ echo "Hello, 42!"
Hello, 42!

minishell$ echo -n "no newline"
no newlineminishell$

minishell$ cd /tmp && pwd
/tmp

minishell$ export MY_VAR="hello world"
minishell$ echo $MY_VAR
hello world

minishell$ unset MY_VAR
minishell$ echo $MY_VAR

minishell$ env | grep PATH
PATH=/usr/local/bin:/usr/bin:/bin

minishell$ exit 42

๐Ÿ”€ Redirections & Pipes

Redirections

# Input redirection โ€” read from file
< infile cat

# Output redirection โ€” write to file (overwrite)
echo "hello" > outfile

# Append redirection โ€” write to file (append)
echo "world" >> outfile

# Here-document โ€” read until delimiter
cat << EOF
This is a heredoc
Multiple lines supported
EOF

Pipes

# Simple pipe
ls -la | grep ".c"

# Multi-pipe
cat file.txt | grep "pattern" | wc -l

# Pipes with redirections
< infile grep "search" | sort | uniq > outfile

Complex Combinations

# Pipe + redirection + built-in
env | grep USER > user_info.txt

# Here-doc + pipe
cat << STOP | wc -l
line one
line two
line three
STOP

# Multiple redirections
< input.txt sort > sorted.txt

๐Ÿš€ Getting Started

Prerequisites

  • GCC compiler
  • GNU Make
  • readline library (libreadline-dev on Debian/Ubuntu)

Installation

# Install readline (if needed)
sudo apt-get install libreadline-dev   # Debian/Ubuntu
brew install readline                   # macOS

# Compile
make

# Run
./minishell

Makefile Targets

make          # Build minishell
make clean    # Remove object files
make fclean   # Remove everything
make re       # Full rebuild

๐ŸŽฎ Usage Examples

# Start the shell
./minishell

# Execute commands like bash
minishell$ ls -la
minishell$ cat /etc/hostname
minishell$ echo "Current dir: $PWD"

# Pipes
minishell$ ls | wc -l
minishell$ cat Makefile | grep "FLAGS" | head -1

# Redirections
minishell$ echo "test" > output.txt
minishell$ cat < output.txt
minishell$ ls -la >> log.txt

# Environment variables
minishell$ export NAME="Adil"
minishell$ echo "Hello $NAME, exit status was $?"

# Exit
minishell$ exit

Quote Handling

# Single quotes โ€” no expansion
minishell$ echo '$USER is not expanded'
$USER is not expanded

# Double quotes โ€” expansion happens
minishell$ echo "$USER is expanded"
adil is expanded

# Mixed quotes
minishell$ echo "Hello '$USER'"
Hello 'adil'

๐Ÿ—๏ธ Architecture

โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚                  MINISHELL PIPELINE               โ”‚
โ”‚                                                  โ”‚
โ”‚  โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”    โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”    โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ” โ”‚
โ”‚  โ”‚  INPUT   โ”‚ โ†’  โ”‚  LEXER   โ”‚ โ†’  โ”‚  PARSER    โ”‚ โ”‚
โ”‚  โ”‚ readline โ”‚    โ”‚ tokenize โ”‚    โ”‚ build AST  โ”‚ โ”‚
โ”‚  โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜    โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜    โ””โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”˜ โ”‚
โ”‚                                        โ”‚        โ”‚
โ”‚                                        โ–ผ        โ”‚
โ”‚  โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”    โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”    โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ” โ”‚
โ”‚  โ”‚  OUTPUT  โ”‚ โ†  โ”‚ EXECUTOR โ”‚ โ†  โ”‚  EXPANDER  โ”‚ โ”‚
โ”‚  โ”‚  stdout  โ”‚    โ”‚ fork+execโ”‚    โ”‚  $VAR โ†’ valโ”‚ โ”‚
โ”‚  โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜    โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜    โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜ โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
  1. Lexer โ†’ Breaks input into tokens (words, operators, quotes)
  2. Parser โ†’ Builds command structure with redirections
  3. Expander โ†’ Resolves $VAR, $?, and quote removal
  4. Executor โ†’ Forks processes, sets up pipes/redirections, calls execve()

๐Ÿ“ก Signal Handling

Signal Interactive Mode During Execution
Ctrl+C (SIGINT) New prompt on new line Interrupts child process
Ctrl+D (EOF) Exits the shell Closes stdin for child
Ctrl+\ (SIGQUIT) Ignored Sends SIGQUIT to child

๐Ÿงช Testing

Quick Functional Tests

# Basic commands
ls -la
echo hello world
cat /etc/hostname

# Pipes
ls | grep Makefile | wc -l

# Redirections
echo test > /tmp/test_minishell
cat < /tmp/test_minishell

# Exit status
ls nonexistent_file
echo $?    # Should print non-zero

# Edge cases
""         # Empty quotes
''         # Empty single quotes
|          # Error: syntax error

Memory Check

valgrind --leak-check=full --show-leak-kinds=all \
  --suppressions=readline.supp ./minishell

The readline.supp file suppresses known readline library leaks that are not our responsibility.

Recommended Testers


๐Ÿ“ Project Structure

minishell/
โ”œโ”€โ”€ include/            # Header files
โ”‚   โ””โ”€โ”€ minishell.h
โ”œโ”€โ”€ parsing/            # Lexer, parser, token handling
โ”‚   โ”œโ”€โ”€ tokenizer.c
โ”‚   โ”œโ”€โ”€ parser.c
โ”‚   โ”œโ”€โ”€ expander.c
โ”‚   โ””โ”€โ”€ quotes.c
โ”œโ”€โ”€ execution/          # Command execution engine
โ”‚   โ”œโ”€โ”€ executor.c
โ”‚   โ”œโ”€โ”€ pipes.c
โ”‚   โ”œโ”€โ”€ redirections.c
โ”‚   โ””โ”€โ”€ builtins.c
โ”œโ”€โ”€ functions_utils1/   # Utility functions
โ”œโ”€โ”€ minishell.c         # Main loop
โ”œโ”€โ”€ readline.supp       # Valgrind suppressions
โ”œโ”€โ”€ Makefile
โ””โ”€โ”€ README.md

๐Ÿ‘ค Author

Adil Bourji โ€” @adi7-x

42 School ยท Common Core ยท Unix ยท Processes

About

Implementation of a simple shell - 42 School

Topics

Resources

Stars

0 stars

Watchers

1 watching

Forks

Releases

Packages

Used by

Contributors

Languages