Skip to content

LazzouziYoussef/MiniShell

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 

Repository files navigation

Minishell

A lightweight, POSIX-compliant Unix shell implementation in C that supports pipelines, redirections, background processes, and signal handling.

Features

Core Functionality

  • Command Execution: Execute any program in your PATH
  • Multi-stage Pipelines: Chain multiple commands together (ls | grep txt | wc -l)
  • I/O Redirection:
    • Output redirection (>)
    • Input redirection (<)
    • Append mode (>>)
  • Background Processes: Run commands asynchronously with &
  • Signal Handling: Proper Ctrl+C handling without terminating the shell
  • Built-in Commands: cd for directory navigation

Technical Highlights

  • Dynamic memory allocation (no arbitrary argument limits)
  • Asynchronous zombie process reaping via SIGCHLD
  • Signal-safe foreground process tracking
  • Proper pipe and file descriptor management
  • Clean error handling and resource cleanup

Getting Started

Prerequisites

  • GCC or any C compiler supporting C99
  • POSIX-compliant Unix-like operating system (Linux, macOS, BSD)

Compilation

gcc -Wall -Wextra -o minishell main.c

For debugging:

gcc -Wall -Wextra -g -o minishell main.c

Running

./minishell

You'll see a prompt:

> 

Usage Examples

Basic Commands

> ls
> pwd
> echo "Hello, World!"

Pipelines

> ls -la | grep .c
> cat file.txt | sort | uniq
> ps aux | grep bash | wc -l

Redirections

> ls > output.txt
> cat < input.txt
> echo "append this" >> log.txt
> cat < input.txt > output.txt

Background Processes

> sleep 10 &
[BG Process: 12345]
> 

Combined Features

> cat file.txt | grep pattern > results.txt
> sort < unsorted.txt | uniq > sorted.txt &

Built-in Commands

> cd /path/to/directory
> cd ~
> cd

Exiting

Press Ctrl+D or close the terminal.

Architecture

Data Structures

Node Structure: Represents a command in the pipeline

struct node {
    int type;           // EXEC, REDIR, PIPE, or BG
    char *cmd_line;     // The command string
    char *file;         // Redirection target file
    int redir_type;     // 1: >, 2: <, 3: >>
    struct node *next;  // Next command in pipeline
};

Main Components

  1. Parser (parse_line, parse_segment): Tokenizes input and builds command tree
  2. Executor (execute_pipeline): Manages process creation, pipes, and file descriptors
  3. Signal Handlers: Manages SIGINT (Ctrl+C) and SIGCHLD (zombie reaping)
  4. Built-ins: Implements shell built-in commands

Control Flow

Input → Parse → Build Node Chain → Execute Pipeline → Wait/Continue
                                          ↓
                               Fork → Setup FDs → Exec

Implementation Details

Process Management

  • Each command in a pipeline runs in its own forked process
  • Parent process tracks foreground process for signal delivery
  • SIGCHLD handler asynchronously reaps terminated background processes

Pipe Management

  • Pre-allocates all pipes before forking
  • Each child inherits all pipe FDs and closes unused ones
  • Proper setup of stdin/stdout redirection for each stage

Memory Management

  • Dynamic allocation using malloc, realloc, and calloc
  • Proper cleanup with free_node() after command execution
  • Uses getline() for arbitrary-length input

Limitations

  • No quote handling (arguments with spaces require external quoting)
  • No environment variable expansion ($VAR)
  • No job control (jobs, fg, bg commands)
  • No command history or line editing
  • No wildcards/globbing (*, ?)
  • cd doesn't work in pipelines (only as standalone command)

Future Enhancements

  • Command history with readline integration
  • Environment variable expansion
  • Quote and escape sequence handling
  • Job control (track background jobs, implement jobs, fg, bg)
  • Wildcard expansion
  • More built-ins (export, unset, exit, history)
  • Tab completion
  • Configuration file support (.minishellrc)

Testing

Basic Tests

# Test simple execution
> ls

# Test pipeline
> echo "test" | cat

# Test redirection
> echo "hello" > test.txt
> cat < test.txt

# Test background
> sleep 5 &

# Test complex pipeline
> cat /etc/passwd | grep root | cut -d: -f1

Edge Cases

# Empty input
> 

# Multiple spaces
>    ls    -la    

# Mixed features
> cat < input.txt | sort | uniq > output.txt &

Contributing

Contributions are welcome! Areas for improvement:

  • Better error messages
  • Additional built-in commands
  • Quote and escape handling
  • Environment variable support
  • Test suite

License

This project is open source and available under the MIT License.

Acknowledgments

Built following POSIX standards and inspired by:

  • The original Unix shell by Ken Thompson
  • xv6 shell implementation
  • Modern shells (bash, zsh, fish)

Author

Created as a systems programming project to demonstrate Unix process management and shell implementation.


Note: This is an educational project demonstrating Unix systems programming concepts. It is not intended as a replacement for production shells like bash or zsh.

About

Minimal C shell (POSIX.1-2008) for learning. Supports command execution, pipelines, input/output/append redirection, background jobs, cd builtin, and basic signal handling. Demonstrates process creation, pipes, and memory management in C..

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages