A lightweight, feature-rich Unix shell implementation written in C. This project demonstrates core operating system concepts including process management, I/O redirection, signal handling, and command parsing.
- Command Execution: Execute any system command available in your PATH
- Built-in Commands: Essential shell commands implemented natively
- Command History: Keep track of previously executed commands
- I/O Redirection: Support for input/output redirection operators
- Background Processes: Run commands in the background
- Signal Handling: Proper handling of Ctrl+C, Ctrl+Z, and other signals
- Colorful UI: Enhanced user experience with colored output
| Command | Description | Usage |
|---|---|---|
cd |
Change directory | cd [directory] |
pwd |
Print working directory | pwd |
echo |
Display a line of text | echo [args...] |
export |
Set environment variable | export VAR=value |
history |
Show command history | history |
clear |
Clear the screen | clear |
help |
Display help information | help |
exit |
Exit the shell | exit [code] |
# Output redirection
command > file.txt # Write output to file (overwrite)
command >> file.txt # Append output to file
# Input redirection
command < input.txt # Read input from file
# Background execution
command & # Run command in backgroundmini-shell/
├── src/
│ ├── main.c # Main shell loop and initialization
│ ├── parser.c # Command parsing and tokenization
│ ├── executor.c # Command execution and process management
│ ├── builtins.c # Built-in command implementations
│ ├── history.c # Command history management
│ └── utils.c # Utility functions and signal handlers
├── include/
│ └── shell.h # Header file with structures and prototypes
├── tests/ # Test files (for future development)
├── docs/ # Documentation (for future development)
├── examples/ # Example scripts
├── Makefile # Build configuration
└── README.md # This file
- GCC compiler (or any C11 compatible compiler)
- Make build system
- POSIX-compliant operating system (Linux, macOS, WSL)
# Build the project
make
# Build with debug symbols
make debug
# Build optimized release version
make release
# Clean build files
make clean
# Rebuild from scratch
make rebuild# Build and run
make run
# Or run directly
./bin/mini-shell# Change directory
mini-shell$ cd /home/user/documents
# Print current directory
mini-shell$ pwd
/home/user/documents
# List files
mini-shell$ ls -la
# Echo text
mini-shell$ echo Hello, World!
Hello, World!# Redirect output to file
mini-shell$ ls -la > files.txt
# Append to file
mini-shell$ echo "New line" >> files.txt
# Read from file
mini-shell$ wc -l < files.txt# Run long-running command in background
mini-shell$ sleep 60 &
[Process 12345 running in background]# Set environment variable
mini-shell$ export MY_VAR=hello
# Use environment variable
mini-shell$ echo $MY_VAR
hello# View command history
mini-shell$ history
1 ls -la
2 cd /home/user
3 pwd
4 echo Hello- Follow standard C coding conventions
- Use meaningful variable and function names
- Add comments for complex logic
- Keep functions focused and modular
- All allocated memory is properly freed
- No memory leaks (verified with valgrind)
- Proper error handling for allocation failures
To add a new built-in command:
- Add function prototype in
include/shell.h - Implement the function in
src/builtins.c - Add command name to
is_builtin()function - Add case in
execute_builtin()function - Update help text in
builtin_help()
# Run with valgrind for memory leak detection
make valgrind
# Run static analysis
make check
# Format code
make format# Install to /usr/local/bin (requires sudo)
make install
# Now you can run from anywhere
mini-shell
# Uninstall
make uninstall- Uses
fork()to create child processes execvp()for command executionwaitpid()for process synchronization- Proper handling of zombie processes
- SIGINT (Ctrl+C): Interrupt current foreground process
- SIGCHLD: Reap zombie processes automatically
- SIGTSTP (Ctrl+Z): Ignored (can be implemented for job control)
- SIGQUIT (Ctrl+\): Ignored
- Uses file descriptors and
dup2()for redirection - Supports both input (
<) and output (>,>>) redirection - Proper error handling for file operations
- Tokenization using
strtok_r() - Support for multiple arguments
- Handles special characters (
>,<,&,|) - Whitespace trimming and empty line detection
Planned features for future versions:
- Pipe support (
command1 | command2) - Job control (fg, bg, jobs commands)
- Command-line editing with arrow keys
- Tab completion
- Command aliases
- Shell scripting support
- Globbing (wildcards: *, ?)
- Conditional execution (
&&,||) - Command substitution
- Configuration file (~/.minishellrc)
This project helps understand:
-
Operating System Concepts
- Process creation and management
- Inter-process communication
- Signal handling
- File descriptors and I/O
-
C Programming
- Memory management
- String manipulation
- Structures and pointers
- Modular code organization
-
System Programming
- POSIX system calls
- Error handling
- Signal safety
- Resource management
# Make sure the binary is executable
chmod +x bin/mini-shell# Verify the command exists in your PATH
which <command>
# Or use absolute path
/bin/ls# Run with valgrind to debug
make valgrindContributions are welcome! Please follow these guidelines:
- Fork the repository
- Create a feature branch
- Write clear commit messages
- Test your changes thoroughly
- Submit a pull request
This project is licensed under the MIT License - see the LICENSE file for details.
Created as an educational project to demonstrate C programming and operating system concepts.
- Inspired by traditional Unix shells (bash, sh, zsh)
- Built with knowledge from operating systems textbooks and courses
- Thanks to the open-source community for resources and inspiration
- Advanced Programming in the UNIX Environment
- The Linux Programming Interface
- GNU C Library Documentation
- POSIX Standards
Happy Coding! 🚀