A custom Unix-like shell implementation written in C++ that provides basic shell functionality with command execution, piping, I/O redirection, and process management.
-
cd [directory]- Change directorycdorcd ~- Go to home directorycd -- Go to previous directorycd <path>- Change to specified directory
-
pwd- Print working directory -
echo [args]- Display text to stdout- Supports quoted strings with proper spacing
-
ls [options] [directories]- List directory contents-a- Show hidden files-l- Long listing format-laor-al- Combination of both flags
-
search <filename>- Search for a file recursively- Returns
Trueif found,Falseotherwise - Uses breadth-first search
- Returns
-
history [n]- Display command historyhistory- Shows last 10 commandshistory <n>- Shows last n commands- Maintains up to 20 commands
- Stores history in
history.txt
-
pinfo [pid]- Display process informationpinfo- Show current process infopinfo <pid>- Show specific process info- Displays: PID, status, memory usage, and executable path
-
fg <pid>- Bring background process to foreground- Sends SIGCONT signal to the specified process
-
exit- Exit the shell
- Semicolon (
;) - Execute multiple commands sequentiallycd /tmp; ls; pwd
- Pipe (
|) - Chain commands togetherls -l | grep txt | wc -l
- Ampersand (
&) - Run commands in backgroundsleep 100 &
- Input (
<) - Redirect input from filesort < input.txt - Output (
>) - Redirect output to filels > output.txt
Any command not recognized as a built-in will be executed as a system command using execvp().
The shell consists of three main components:
main.cpp- Main shell loop and command dispatcherutils.cpp- Implementation of all built-in commands and utilitiesheaders.h- Header file with includes and function declarations
processCommand()- Handles individual command executionparseCommandTree()- Parses piped commands into linked list structureexecuteCommandTree()- Executes piped commands with proper file descriptor management
- Uses
fork()andexecvp()for command execution - Implements proper process groups with
setsid() - Handles foreground and background processes
- Tracks process status (Running, Sleeping, Zombie, Stopped)
appendToHistory()- Adds commands to history (max 20)printHistory()- Displays command history- Avoids duplicate consecutive entries
- Persistent storage in
history.txt
searchFile()- BFS-based file searchlistDirectory()- Directory listing with detailed info- Supports I/O redirection with file descriptors
- C++ compiler with C++17 support (g++)
- macOS (uses macOS-specific libraries like
libproc.h) - Make build system
makeThis will compile the source files and create the executable myprogram.
./myprogrammake cleancustom-C-shell/
├── headers.h # Header file with declarations and includes
├── main.cpp # Main shell loop and command parsing
├── utils.cpp # Implementation of built-in commands
├── Makefile # Build configuration
├── history.txt # Command history storage
└── README.md # This file
# Basic navigation
priyanshu@MacBook:~> pwd
/Users/priyanshusharma/Documents/AOS_Assignment2
# List files with details
priyanshu@MacBook:~> ls -la
# Chain commands
priyanshu@MacBook:~> cd /tmp; ls; pwd
# Pipe commands
priyanshu@MacBook:~> ls -l | grep cpp
# Background process
priyanshu@MacBook:~> sleep 30 &
Started background process with PID: 12345
# Process information
priyanshu@MacBook:~> pinfo 12345
# Command history
priyanshu@MacBook:~> history 5
# File search
priyanshu@MacBook:~> search main.cpp
True
# I/O redirection
priyanshu@MacBook:~> ls > files.txt
priyanshu@MacBook:~> grep cpp < files.txt- Platform-specific: Currently designed for macOS (uses
libproc.h,proc_pidinfo, etc.) - Hardcoded paths: Some paths are hardcoded (e.g., home directory path in
getRelativePath()) - Limited error handling: Some edge cases may not be fully handled
- No job control: Limited support for complex job control features
- Cross-platform support (Linux, Windows)
- Tab completion
- Command history navigation with arrow keys
- More robust signal handling
- Configuration file support
- Aliases and shell functions
- Improved job control (bg, jobs commands)
- Scripting support
Priyanshu Sharma
This project is open source and available for educational purposes.