Skip to content

A lightweight Unix shell built in C, inspired by bash. Supports pipes, redirections, heredocs, environment variable expansion, signal handling, and built-in commands — with a custom garbage collector for memory and fd management. (42 project)

Notifications You must be signed in to change notification settings

MrMsnawi/minishell

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

171 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Minishell

A simplified Unix shell written in C, inspired by bash. This project recreates core shell functionality including command execution, piping, redirections, environment variable expansion, heredocs, and built-in commands.

Features

  • Interactive prompt with command history (via GNU Readline)
  • Command execution with PATH resolution
  • Pipes (|) to chain commands
  • Redirections
    • Input: <
    • Output: >
    • Append: >>
    • Heredoc: <<
  • Environment variable expansion ($VAR, $?)
  • Quote handling — single (') and double (") quotes
  • Signal handlingCtrl+C, Ctrl+D, Ctrl+\
  • Garbage collector — automatic memory and file descriptor tracking
  • Built-in commands:
Command Description
echo Print text (supports -n flag)
cd Change directory
pwd Print working directory
export Set/append environment variables
unset Remove environment variables
env Display environment variables
exit Exit the shell

Requirements

  • OS: Linux
  • Compiler: cc (GCC/Clang) with C99 support
  • Library: libreadline-dev

Install dependencies (Debian/Ubuntu)

sudo apt-get install libreadline-dev

Build

make        # Build the project
make clean  # Remove object files
make fclean # Remove object files and binary
make re     # Full rebuild

Usage

./minishell

The shell does not accept command-line arguments. Once launched, it presents an interactive prompt:

minishell$ echo hello world
hello world
minishell$ ls -la | grep minishell | wc -l
1
minishell$ cat << EOF
> line 1
> line 2
> EOF
line 1
line 2
minishell$ export FOO=bar
minishell$ echo $FOO
bar
minishell$ exit

Project Structure

.
├── Makefile
├── include/
│   └── minishell.h            # Header with all structs, prototypes, and macros
├── main/
│   └── minishell.c            # Entry point and main loop
├── parsing/
│   ├── parsing.c              # Parsing orchestration
│   ├── tokenize.c             # Input tokenization
│   ├── verify_syntax.c        # Syntax validation (quotes, pipes, redirections)
│   ├── expand_all.c           # Environment variable expansion
│   ├── expand_redir.c         # Redirection target expansion
│   ├── handle_redir.c         # Redirection extraction from input
│   ├── fill_cmd.c             # Build command structs from tokens
│   ├── del_restore_qo.c       # Quote removal
│   ├── ft_smart_split.c       # Quote-aware string splitting
│   └── ...
├── execution/
│   ├── execution.c            # Execution pipeline (fork, pipe, wait)
│   ├── execute_cmd.c          # External command execution (execve)
│   ├── signals.c              # Signal handlers
│   ├── builtins/              # Built-in command implementations
│   ├── env/                   # Environment variable management
│   ├── redirections/          # Redirection and heredoc handling
│   ├── sys_calls/             # Safe wrappers for system calls
│   └── tools/                 # PATH resolution, error reporting
├── strings/                   # String utility functions
└── garbage_collector/         # Memory and fd tracking

About

A lightweight Unix shell built in C, inspired by bash. Supports pipes, redirections, heredocs, environment variable expansion, signal handling, and built-in commands — with a custom garbage collector for memory and fd management. (42 project)

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published