A robust, custom Unix shell implementation in C, designed for educational purposes and practical use. It features a REPL loop, command execution, I/O redirection, piping, background processes, and more.
- REPL Interface: Interactive Read-Eval-Print Loop with a custom prompt.
- Command Execution: Seamlessly executes external system commands.
- Built-in Commands: Native support for
cd(change directory) andexit. - Cross-Platform: Runs on POSIX systems (Linux, macOS) and Windows (MinGW).
- I/O Redirection:
- Input (
<): Read from files. - Output (
>): Write to files. - Combined:
sort < input.txt > output.txt.
- Input (
- Piping:
- Chain multiple commands:
cmd1 | cmd2 | cmd3. - Supports unlimited pipe depth.
- Chain multiple commands:
- Background Execution:
- Run commands asynchronously using
&. - Displays PID for background jobs.
- Run commands asynchronously using
- Signal Handling:
- Ctrl+C Protection: The shell ignores SIGINT, preventing accidental termination.
- Graceful Interruption: Foreground commands can be interrupted without killing the shell.
- Configuration:
- Loads startup commands from
~/.myshellrc. - Customize your environment automatically on launch.
- Loads startup commands from
- Robust Error Handling:
- Consistent, informative error messages.
- Detailed system error reporting (errno).
- Command History:
- Use Up/Down Arrows to navigate previous commands.
- History persists for the session.
- Job Control (POSIX only):
jobs: List all background and stopped jobs.fg: Bring a job to the foreground.bg: Continue a stopped job in the background.- Track and manage background processes.
- Tab Completion:
- Press Tab to auto-complete commands, files, and directories.
- Shows all matches if multiple options exist.
- Completes to common prefix when possible.
- Environment Variable Expansion:
- Supports
$VARand${VAR}syntax. - Variables are expanded in all command arguments.
- Use
\$to escape dollar signs.
- Supports
- GCC Compiler
- Make (optional)
./build.shmkdir -p obj
gcc -Wall -Wextra -Iinclude -c src/main.c -o obj/main.o
gcc -Wall -Wextra -Iinclude -c src/builtins.c -o obj/builtins.o
gcc -Wall -Wextra -Iinclude -c src/error.c -o obj/error.o
gcc -Wall -Wextra -Iinclude -c src/readline.c -o obj/readline.o
gcc -Wall -Wextra -Iinclude -c src/jobs.c -o obj/jobs.o
gcc obj/main.o obj/builtins.o obj/error.o obj/readline.o obj/jobs.o -o myshellStart the shell:
./myshellFile Operations
myshell> ls -la
myshell> pwd
myshell> cat README.mdEnvironment Variables
myshell> echo $HOME
myshell> echo ${USER}
myshell> cd $HOME/Documents
myshell> cat $HOME/.bashrcTab Completion
myshell> ec[TAB] # Completes to "echo"
myshell> cd Doc[TAB] # Completes to "Documents/" if it exists
myshell> cat RE[TAB] # Shows: README.md RESULTS.txtRedirection & Piping
# Redirect output
myshell> echo "Hello" > file.txt
# Pipe commands
myshell> cat file.txt | grep "H" | wc -l
# Complex pipeline
myshell> ps aux | grep user | sort | head -5Background Jobs
myshell> sleep 10 &
[1] 12345
myshell> jobs
[1]+ Running sleep 10
myshell> fg 1
sleep 10
# Press Ctrl+Z to stop
[1]+ Stopped sleep 10
myshell> bg 1
[1]+ sleep 10 &Configuration
Create a ~/.myshellrc file to run commands at startup:
# ~/.myshellrc
echo "Welcome to myshell!"
dateThe project is structured for modularity and maintainability:
myshell/
├── src/
│ ├── main.c # Core logic: REPL, parser, executor
│ ├── builtins.c # Built-in command implementations
│ ├── error.c # Centralized error handling
│ ├── readline.c # Command history and input handling
│ └── jobs.c # Job control system
├── include/
│ ├── builtins.h # Headers for built-ins
│ ├── error.h # Headers for error handling
│ ├── readline.h # Headers for readline
│ └── jobs.h # Headers for job control
├── obj/ # Compiled object files
├── build.sh # Build automation script
└── README.md # Documentation
- Tokenizer: Splits input strings into tokens using
strtok. - Parser: Converts tokens into
struct commandobjects, handling redirection and background flags. - Pipeline Splitter: Breaks command chains by the pipe symbol
|. - Executor:
- POSIX: Uses
fork(),pipe(),dup2(), andexecvp()for full functionality. - Windows: Uses
_spawnvp()with platform-specific adaptations.
- POSIX: Uses
- Signal Handler: Manages
SIGINTto protect the shell process. - Job Manager: Tracks background jobs, handles
jobs,fg, andbgcommands. - Tab Completion: Auto-completes commands and file paths using Windows FindFirstFile API.
- Variable Expansion: Expands
$VARand${VAR}environment variables in commands.
- Scripting: No support for control structures like
iforwhile. - Windows Job Control:
fgandbgcommands not supported on Windows. - Variable Assignment: Cannot set/export custom variables (only expansion supported).
Contributions are welcome! Feel free to submit pull requests or open issues for bugs and feature ideas.
This project is open-source and available for educational and personal use.