Experience the power of a custom shell with advanced features like piping, I/O redirection, and background processes
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 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 |
- ๐ Learn how operating systems work internally
- ๐ง Understand process management and system calls
- ๐ Customize your command-line experience
- ๐ช Master low-level programming concepts
|
|
- 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 --installgit clone https://github.com/Het-2004/Build-your-own-shell.git
cd Build-your-own-shell
gcc -o advanced_shell advanced_shell.c./advanced_shellYou'll see the beautiful prompt:
๐ /current/working/directory >
# List files
๐ ~/projects > ls -la
# Show current directory
๐ ~/projects > pwd
# Echo text
๐ ~/projects > echo "Hello, Advanced Shell!"| Command | Description | Example |
|---|---|---|
cd |
Change directory | cd /home/user/Documents |
help |
Show help menu | help |
exit |
Exit the shell | exit |
# 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# 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# 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!"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
- ๐ 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
# 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# 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๐ Process Management
- Uses
fork()for creating child processes execvp()for command execution with PATH resolutionwaitpid()for proper process synchronization- Zombie process prevention with
SIGCHLDhandling
๐ 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
| 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 |
Edit the prompt in shell_loop():
printf("๐ %s > ", cwd); // Current prompt
printf("โญ %s $ ", cwd); // Alternative style
printf("[%s] โ ", cwd); // Minimal style- Add function declaration
- Implement the function
- Add to
builtin_str[]array - Add to
builtin_func[]array
โ 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
We welcome contributions! Here's how to get started:
- ๐ด Fork the repository
- ๐ฟ Create a feature branch (
git checkout -b feature/amazing-feature) - โจ Commit your changes (
git commit -m 'Add amazing feature') - ๐ค Push to the branch (
git push origin feature/amazing-feature) - ๐ Open a Pull Request
- ๐ Multiple pipe support
- ๐ Command history
- โจ๏ธ Tab completion
- ๐ Environment variables
- ๐ Script execution
- ๐ฎ Job control commands
- ๐ 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,bgcommands - ๐ Aliases - Custom command shortcuts
- โ๏ธ Config Files -
.shellrcsupport
This project is licensed under the MIT License - see the LICENSE file for details.
Special thanks to:
- ๐ง Unix Philosophy - Simple, composable tools
- ๐ POSIX Standards - Cross-platform compatibility
- ๐ Educational Goals - Understanding OS concepts
- ๐ Open Source Community - Inspiration and support