Skip to content

๐Ÿš€ Advanced Unix shell implementation in C featuring command execution, piping, I/O redirection, background processes, and built-in commands. Educational project for learning OS concepts and system programming.

License

Notifications You must be signed in to change notification settings

Het-2004/Build-your-own-shell

Folders and files

NameName
Last commit message
Last commit date

Latest commit

ย 

History

2 Commits
ย 
ย 
ย 
ย 
ย 
ย 

Repository files navigation

๐Ÿš€ C Shell

A powerful Unix shell implementation with modern features

Language Platform License Build

Experience the power of a custom shell with advanced features like piping, I/O redirection, and background processes


๐Ÿค” What is a Shell?

A shell is a command-line interpreter that provides a user interface for accessing an operating system's services. Think of it as the bridge between you and your computer's core functions.

๐Ÿ–ฅ๏ธ Shell vs Terminal vs Command Prompt

Shell Type Platform Description
๐Ÿง Bash Linux/macOS Most popular Unix shell
๐Ÿš Zsh Linux/macOS Enhanced shell with modern features
๐Ÿ”ต PowerShell Windows Microsoft's advanced shell
โšซ CMD Windows Traditional Windows command prompt
๐Ÿš€ Our C Shell Unix/Linux Custom implementation with modern features

๐Ÿ’ก Why Build a Custom Shell?

  • ๐ŸŽ“ Learn how operating systems work internally
  • ๐Ÿ”ง Understand process management and system calls
  • ๐Ÿš€ Customize your command-line experience
  • ๐Ÿ’ช Master low-level programming concepts

โœจ Features

๐ŸŽฏ Core Functionality

  • โšก Fast command execution
  • ๐Ÿ”„ Interactive shell loop
  • ๐Ÿ“ Dynamic directory display
  • ๐Ÿ›ก๏ธ Signal handling (Ctrl+C)
  • ๐Ÿงน Automatic process cleanup

๐Ÿ”ง Advanced Features

  • ๐Ÿ”€ Piping - Chain commands (|)
  • ๐Ÿ“ค I/O Redirection - <, >, >>
  • ๐Ÿ”™ Background Jobs - Run with &
  • ๐Ÿ’ป Built-in Commands - cd, help, exit
  • ๐ŸŽจ Colored Prompt - Shows current directory

๐Ÿš€ Quick Start

Prerequisites

โš ๏ธ Platform Requirements:

  • Unix-like Operating System (Linux, macOS, BSD)
  • Or Windows with WSL (Windows Subsystem for Linux)
  • Not compatible with native Windows

๐Ÿ“ฆ Build Requirements:

# Ensure you have GCC installed
gcc --version

# On Ubuntu/Debian
sudo apt install build-essential

# On macOS
xcode-select --install

# On Windows - Install WSL first
wsl --install

๐Ÿ”จ Compilation

git clone https://github.com/Het-2004/Build-your-own-shell.git
cd Build-your-own-shell
gcc -o advanced_shell advanced_shell.c

๐ŸŽฎ Run the Shell

./advanced_shell

You'll see the beautiful prompt:

๐Ÿš€ /current/working/directory > 

๐Ÿ“– Usage Guide

๐Ÿ  Basic Commands

# List files
๐Ÿš€ ~/projects > ls -la

# Show current directory
๐Ÿš€ ~/projects > pwd

# Echo text
๐Ÿš€ ~/projects > echo "Hello, Advanced Shell!"

๐Ÿ”ง Built-in Commands

Command Description Example
cd Change directory cd /home/user/Documents
help Show help menu help
exit Exit the shell exit

๐Ÿ”€ Piping Magic

# Find all C files
๐Ÿš€ ~/project > ls -la | grep "\.c$"

# Count lines in files
๐Ÿš€ ~/project > cat *.txt | wc -l

# Search and sort
๐Ÿš€ ~/project > grep "function" *.c | sort

๐Ÿ“ I/O Redirection

# Save output to file
๐Ÿš€ ~/project > ls -la > file_list.txt

# Append to file
๐Ÿš€ ~/project > echo "New entry" >> file_list.txt

# Read from file
๐Ÿš€ ~/project > wc -l < large_file.txt

# Combine both
๐Ÿš€ ~/project > sort < input.txt > sorted_output.txt

๐Ÿ”™ Background Processing

# Run long process in background
๐Ÿš€ ~/project > sleep 60 &
Started background job with PID: 12345

# Continue using shell immediately
๐Ÿš€ ~/project > ls
๐Ÿš€ ~/project > echo "Shell is still responsive!"

๐Ÿ—๏ธ Architecture

graph TD
    A[User Input] --> B[Shell Loop]
    B --> C{Parse Command}
    C --> D[Built-in Command?]
    D -->|Yes| E[Execute Built-in]
    D -->|No| F[Fork Process]
    F --> G{Redirection?}
    G -->|Yes| H[Setup I/O]
    G -->|No| I[Execute Command]
    H --> I
    I --> J{Pipe?}
    J -->|Yes| K[Create Pipe]
    J -->|No| L[Wait/Background]
    K --> L
    L --> B
    E --> B
Loading

๐Ÿงฉ Core Components

  • ๐Ÿ”„ Shell Loop: Main interactive loop handling user input
  • โš™๏ธ Command Parser: Tokenizes and processes command strings
  • ๐Ÿš€ Process Manager: Handles fork/exec for external commands
  • ๐Ÿ”€ Pipe Handler: Manages inter-process communication
  • ๐Ÿ“ I/O Manager: Handles file redirection operations
  • ๐Ÿ›ก๏ธ Signal Handler: Manages system signals gracefully

๐ŸŽฏ Advanced Examples

๐Ÿ”ฅ Power User Commands

# Complex pipeline with redirection
๐Ÿš€ ~/logs > cat access.log | grep "ERROR" | sort | uniq -c > error_summary.txt

# Background compilation
๐Ÿš€ ~/project > make all > build.log 2>&1 &

# Multi-step data processing
๐Ÿš€ ~/data > cat raw_data.csv | cut -d',' -f2 | sort -n | tail -10

๐Ÿงช Testing Your Shell

# Test I/O redirection
echo "test content" > test.txt
cat < test.txt
cat test.txt | wc -l

# Test background processes
sleep 5 &
ps aux | grep sleep

# Test piping
ls -la | head -5 | tail -3

๐Ÿ” Technical Deep Dive

๐Ÿ› ๏ธ Implementation Highlights

๐Ÿ“‹ Process Management
  • Uses fork() for creating child processes
  • execvp() for command execution with PATH resolution
  • waitpid() for proper process synchronization
  • Zombie process prevention with SIGCHLD handling
๐Ÿ”€ Pipe Implementation
  • Creates anonymous pipes with pipe()
  • Proper file descriptor duplication with dup2()
  • Handles both read and write ends correctly
  • Supports single-pipe commands (extensible to multiple)
๐Ÿ“ I/O Redirection
  • File descriptor manipulation for stdin/stdout
  • Support for input (<), output (>), and append (>>)
  • Proper file opening with appropriate flags
  • Error handling for file operations

๐Ÿ“Š Performance Characteristics

Feature Performance Memory Usage
Command Parsing O(n) Fixed buffers
Process Creation Fork overhead Minimal
I/O Redirection File I/O speed Low overhead
Pipe Operations Kernel buffered Efficient

๐ŸŽจ Customization

๐ŸŽฏ Modify the Prompt

Edit the prompt in shell_loop():

printf("๐Ÿš€ %s > ", cwd);  // Current prompt
printf("โญ %s $ ", cwd);  // Alternative style
printf("[%s] โ†’ ", cwd);   // Minimal style

๐Ÿ”ง Add Built-in Commands

  1. Add function declaration
  2. Implement the function
  3. Add to builtin_str[] array
  4. Add to builtin_func[] array

๐Ÿ› Troubleshooting

Common Issues

โŒ Compilation Errors
# Missing headers
sudo apt-get install libc6-dev

# GCC not found
sudo apt-get install gcc

# Permission denied
chmod +x advanced_shell
๐Ÿ”ง Runtime Issues
  • Command not found: Check PATH variable
  • Permission denied: Verify file permissions
  • Segmentation fault: Check command length limits
  • Zombie processes: Built-in SIGCHLD handling

๐Ÿค Contributing

We welcome contributions! Here's how to get started:

๐ŸŒŸ How to Contribute

  1. ๐Ÿด Fork the repository
  2. ๐ŸŒฟ Create a feature branch (git checkout -b feature/amazing-feature)
  3. โœจ Commit your changes (git commit -m 'Add amazing feature')
  4. ๐Ÿ“ค Push to the branch (git push origin feature/amazing-feature)
  5. ๐Ÿ”„ Open a Pull Request

๐ŸŽฏ Areas for Contribution

  • ๐Ÿ”„ Multiple pipe support
  • ๐Ÿ“š Command history
  • โŒจ๏ธ Tab completion
  • ๐ŸŒ Environment variables
  • ๐Ÿ“œ Script execution
  • ๐ŸŽฎ Job control commands

๐Ÿ›ฃ๏ธ Roadmap

๐Ÿš€ Upcoming Features

  • ๐Ÿ”„ Multiple Pipes - Support cmd1 | cmd2 | cmd3
  • ๐Ÿ“š Command History - Arrow key navigation
  • โŒจ๏ธ Tab Completion - Auto-complete commands and files
  • ๐ŸŒ Variables - Environment variable expansion
  • ๐Ÿ“œ Scripting - Execute shell scripts
  • ๐ŸŽฎ Job Control - jobs, fg, bg commands
  • ๐Ÿ”— Aliases - Custom command shortcuts
  • โš™๏ธ Config Files - .shellrc support

๐Ÿ“„ License

This project is licensed under the MIT License - see the LICENSE file for details.


๐Ÿ™ Acknowledgments

Special thanks to:

  • ๐Ÿ”ง Unix Philosophy - Simple, composable tools
  • ๐Ÿ“š POSIX Standards - Cross-platform compatibility
  • ๐ŸŽ“ Educational Goals - Understanding OS concepts
  • ๐ŸŒŸ Open Source Community - Inspiration and support

๐ŸŒŸ Star this repository if you found it helpful! โญ

Made with โค๏ธ for learning and education

GitHub stars GitHub forks

About

๐Ÿš€ Advanced Unix shell implementation in C featuring command execution, piping, I/O redirection, background processes, and built-in commands. Educational project for learning OS concepts and system programming.

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages