A custom Unix shell implementation written in C++ with advanced features including command parsing, I/O redirection, pipes, background processes, and built-in commands.
- Command Execution: Execute system commands with full argument support
- Pipes: Chain commands using pipes (
|) - I/O Redirection:
- Output redirection (
>,>>) - Input redirection (
<) - Error redirection (
2>,2>>) - Combined output and error redirection (
>&,>>&)
- Output redirection (
- Background Processes: Run commands in background using
& - Process Substitution: Advanced process substitution support
cd [directory]- Change directory with support for relative/absolute paths and~expansionsetenv VARIABLE VALUE- Set environment variablesunsetenv VARIABLE- Remove environment variablesprintenv [VARIABLE]- Display environment variablessource FILE- Execute commands from a fileexit- Exit the shell gracefully
- Wildcard Expansion: Support for
*and?wildcards in filenames - Environment Variable Expansion:
$VARIABLE- Standard variable expansion$_- Last argument of previous command$?- Exit status of last command$$- Process ID of shell$!- Process ID of last background command$SHELL- Path to current shell
- Tilde Expansion:
~expands to home directory,~userexpands to user's home - Command History: Interactive line editing with history
- Signal Handling: Proper handling of Ctrl-C (SIGINT) and child process cleanup (SIGCHLD)
- Shell Configuration: Automatic execution of
.shellrcon startup
- Custom Prompt: Configurable via
PROMPTenvironment variable - Line Editing: Interactive command line editing with terminal control
- TTY Detection: Behaves differently when input is from terminal vs file/pipe
- GCC/G++ compiler with C++17 support
- Flex (lexical analyzer generator)
- Yacc/Bison (parser generator)
- Make build system
# Clone the repository
git clone <repository-url>
cd Shell
# Build the shell
make
# Optional: Enable debug printing
make PRINTING=1
# Clean build files
make clean# Start interactive shell
./shell
# Run shell script
./shell < script.sh
# Execute commands from file
echo "ls -la" | ./shellCreate a .shellrc file in your working directory for automatic startup commands:
# Example .shellrc
setenv PATH /usr/bin:/bin:/usr/local/bin
setenv PROMPT "myshell> "
cd /home/user# Basic command execution
myshell> ls -la
# Pipes and redirection
myshell> cat file.txt | grep "pattern" > output.txt
# Background processes
myshell> long_running_command &
# Environment variables
myshell> setenv MY_VAR "hello world"
myshell> echo $MY_VAR
# Wildcards
myshell> ls *.cpp
# Change directory
myshell> cd ~/Documents
myshell> cd ..
# Source script
myshell> source setup.sh-
Lexical Analyzer (
lexical_analyzer.l)- Tokenizes input using Flex
- Handles quotes, escapes, and special characters
- Implements environment variable and wildcard expansion
-
Syntax Parser (
syntax_parser.y)- Parses command syntax using Yacc/Bison
- Builds command structures for execution
- Handles operator precedence and grammar rules
-
Command Processor (
command_processor.cpp/.hh)- Executes parsed commands
- Manages process creation and communication
- Handles I/O redirection and pipes
-
Simple Command (
simple_command.cpp/.hh)- Represents individual commands with arguments
- Manages argument lists and command structure
-
Shell Interpreter (
shell_interpreter.cpp/.hh)- Main shell loop and initialization
- Signal handling and process management
- Prompt display and user interaction
-
Terminal Control (
terminal_control.cpp)- Low-level terminal operations
- Enables advanced line editing features
-
Line Editor (
line_editor.cpp)- Interactive command line editing
- Command history and completion
- SIGINT (Ctrl-C): Interrupts current command, returns to prompt
- SIGCHLD: Cleans up zombie processes from background commands
- Proper cleanup of dynamic allocations
- RAII principles for resource management
- Exception-safe command execution
Shell/
├── lexical_analyzer.l # Flex lexical analyzer
├── syntax_parser.y # Yacc/Bison parser
├── shell_interpreter.* # Main shell implementation
├── command_processor.* # Command execution engine
├── simple_command.* # Command data structures
├── terminal_control.cpp # Terminal I/O operations
├── line_editor.cpp # Interactive editing
└── Makefile # Build configuration
The Makefile supports:
- Conditional compilation flags (
PRINTING=1for debug output) - Automatic dependency tracking
- Clean builds and intermediate file management
- Optional line editing mode compilation
Run the shell with various command combinations to test functionality:
- Complex pipe chains
- Multiple redirections
- Background process management
- Environment variable operations
- Wildcard expansions
- Error handling scenarios