Skip to content

A minimal UNIX shell for the 42 curriculum. Supports built-in commands, redirections, pipes, environment variables, and signal handling—crafted in pure C.

Notifications You must be signed in to change notification settings

Aztaban/minishell_MD

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

146 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Minishell

A 42 project focused on building a minimal Unix shell. The goal is to reproduce core shell behavior: parsing, quoting rules, redirections, pipelines, environment handling, builtins, and correct signal behavior in interactive mode.

🚀 Overview

  • This minishell implements:
  • Interactive prompt using GNU Readline
  • Tokenization + parsing into an AST
  • Execution of builtins and external commands
  • Pipelines (|) and redirections (<, >, >>)
  • Heredoc (<<) with proper Ctrl+C behavior
  • Environment variables ($VAR, $?) and expansion rules
  • Correct signal handling similar to bash (parent vs child processes)

✅ Supported features

Builtins

  • cd
  • echo (with -n)
  • env
  • exit
  • export
  • pwd
  • unset

Operators / Syntax

  • Pipes: cmd1 | cmd2 | cmd3
  • Redirections:
    • input: < file
    • output: > file
    • append: >> file
  • Heredoc:
    • cmd << DELIMITER
    • stops on delimiter
    • Ctrl+C aborts heredoc and returns to prompt

Expansions

  • $VAR
  • $? (last exit status)
  • Expansion inside double quotes, no expansion inside single quotes

Signals (bash-like behavior)

  • Parent shell:
    • Ctrl+C prints newline and shows a fresh prompt
    • Ctrl+\ ignored
  • Child processes:
    • default signal behavior (so Ctrl+C / Ctrl+\ affect running commands normally)
  • Heredoc:
    • Ctrl+C aborts heredoc collection and sets status to 130

📁 Folder structure

├── docs/           # Project documentation
├── include/        # Public header
├── libft/          # Custom Libft library
├── src/
│   ├── builtins/   # Builtin command implementations
│   ├── env/        # Environment handlin
│   ├── executor/   # Command execution: AST execution, pipes, redirections, heredoc 
│   ├── parser/     # Lexing, parsing, tokenization, expansion
│   ├── utils/      # Cleanup, signal handlers
│   └── main.c      # Program entry point + main interactive loop (readline → parse → execute)
├── readline.supp   # Valgrind suppression file for GNU Readline-related “leaks”
└── Makefile        # Build rules

🧠 Execution flow (high level)

1. Read input

Uses readline() for interactive input and history support.

2. Lexing (tokenization)

Splits the input into tokens while respecting quotes and operators.

3. Expansion

Expands environment variables and $? (exit status).

4. Parsing

Builds an AST / execution structure (commands, pipes, redirections).

5. Execution

  • Collects heredocs before execution
  • Runs builtins in the parent when needed (e.g., cd, exit)
  • Forks and executes external commands
  • Handles pipe chains and redirections

🛠️ Compilation

make            # builds minishell
make clean      # removes object files
make fclean     # removes binaries and objects
make re         # rebuilds everything

Usage

Run:

./minishell

Examples:

minishell$ echo hello
hello

minishell$ export NAME=Martin
minishell$ echo $NAME
Martin

minishell$ ls -l | grep minishell
...

minishell$ cat < infile | wc -l > out.txt

Heredoc

minishell$ cat << EOF
> hello
> world
> EOF
hello
world

🛠️ Notes / Limitations

  • This project targets the mandatory minishell requirements from 42.
  • Only required shell grammar and behaviors are implemented (not a full bash clone).
  • Behavior may differ from bash in edge cases not required by the subject.

🧪 Valgrind

This project includes two Makefile targets for running Valgrind with a readline suppression file:

Run Valgrind in the terminal

make valgrind

Runs Valgrind with leak checking:

valgrind --leak-check=full --track-origins=yes --suppressions=readline.supp ./minishell

Run Valgrind and write a full report to valgrind.log

make log

Runs Valgrind and writes output into valgrind.log:

valgrind --leak-check=full --log-file=valgrind.log --track-origins=yes \
  --show-leak-kinds=all --suppressions=readline.supp ./minishell

Note: if you press Ctrl+C while running minishell through make log, GNU make may display Interrupt. This happens because Ctrl+C is delivered to the whole foreground job (make + valgrind + minishell). For signal testing, running Valgrind directly is recommended.

👤 Authors

Created and maintained by Martin Justa and Diana Kolarova as part of the 42 school curriculum.

About

A minimal UNIX shell for the 42 curriculum. Supports built-in commands, redirections, pipes, environment variables, and signal handling—crafted in pure C.

Topics

Resources

Stars

Watchers

Forks

Contributors 2

  •  
  •