Skip to content

Maroua-netizen/Minishell

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

286 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Minishell

A simple UNIX shell implementation written in C, inspired by bash. This project recreates the functionality of a basic command-line interpreter with support for command execution, pipelines, redirections, environment variables, and built-in commands.

Features

Core Functionality

  • Interactive prompt: Display a command prompt and wait for user input
  • Command execution: Execute commands found in PATH or by absolute/relative path
  • Argument handling: Pass arguments to executed commands
  • Command history: Navigate through command history using arrow keys

Redirections

  • < - Input redirection
  • > - Output redirection (overwrite)
  • >> - Output redirection (append)
  • << - Heredoc (read input until delimiter)

Pipelines

  • Chain multiple commands using the pipe operator |
  • Support for multiple pipes in a single command line

Environment Variables

  • Variable expansion with $VARIABLE
  • $? - Exit status of the last executed command
  • Quote handling:
    • Single quotes ' - Preserve literal value
    • Double quotes " - Allow variable expansion

Built-in Commands

  • echo - Display text (with -n flag support)
  • cd - Change directory (relative or absolute path)
  • pwd - Print working directory
  • export - Set environment variables
  • unset - Remove environment variables
  • env - Display environment variables
  • exit - Exit the shell

Signal Handling

  • Ctrl+C - Interrupt current command
  • Ctrl+D - Exit shell (EOF)
  • Ctrl+\ - Quit (ignored in interactive mode)

Installation

Prerequisites

  • GCC or compatible C compiler
  • Make
  • readline library (development files)

Ubuntu/Debian

sudo apt-get update
sudo apt-get install build-essential libreadline-dev

Build

make

This will create the minishell executable.

Clean

make clean   # Remove object files
make fclean  # Remove object files and executable
make re      # Rebuild everything

Usage

Starting the Shell

./minishell

Example Commands

# Simple command execution
minishell$ ls -la

# Pipelines
minishell$ cat file.txt | grep "pattern" | wc -l

# Redirections
minishell$ echo "Hello World" > output.txt
minishell$ cat < input.txt
minishell$ echo "Append text" >> output.txt

# Heredoc
minishell$ cat << EOF
> line 1
> line 2
> EOF

# Environment variables
minishell$ export MY_VAR="Hello"
minishell$ echo $MY_VAR
minishell$ echo "Exit status: $?"

# Built-in commands
minishell$ pwd
minishell$ cd /tmp
minishell$ env
minishell$ unset MY_VAR
minishell$ exit

Project Structure

.
├── Makefile              # Build configuration
├── minishell.h           # Header file with all declarations
├── main.c                # Entry point and main loop
├── execution/            # Command execution logic
│   ├── exec.c           # Command execution
│   ├── pipeline.c       # Pipeline handling
│   ├── redirection.c    # I/O redirections
│   ├── heredoc.c        # Heredoc implementation
│   ├── signals.c        # Signal handling
│   └── built-in/        # Built-in commands
│       ├── echo.c
│       ├── cd.c
│       ├── pwd.c
│       ├── export.c
│       ├── env.c
│       ├── exit.c
│       ├── unset.c
│       └── builtin.c
├── parsing/              # Input parsing and tokenization
│   ├── lexer/           # Lexical analysis (tokenization)
│   ├── parser/          # Syntax parsing (AST construction)
│   ├── expansion.c      # Variable expansion
│   └── env_parsing.c    # Environment parsing
├── libft/                # Custom C library functions (string, memory utils)
└── src/                  # Utility functions (error handling, memory management)

Technical Details

Memory Management

  • All dynamically allocated memory is properly freed
  • No memory leaks (verified with valgrind)

Error Handling

  • Comprehensive error checking for system calls
  • Meaningful error messages for user feedback

Exit Status

  • Proper exit status handling and propagation
  • Accessible via $? variable

Authors

  • mmounsif
  • delhajou

Acknowledgments

This project is part of the 42 School curriculum. It provides hands-on experience with:

  • Process management (fork, execve, wait)
  • File descriptors and I/O redirection
  • Signal handling
  • Parsing and lexical analysis
  • Memory management in C

License

This project is created for educational purposes as part of the 42 School curriculum.

About

A minimal Unix shell in C, recreating essential Bash features and system behavior.

Topics

Resources

Stars

Watchers

Forks

Contributors