MYSH — Advanced Unix Shell (C)
MYSH Unix Shell POSIX-Style Command Line Shell Built in C Summary MYSH is a compact POSIX-style Unix command-line shell implemented in…
MYSH Unix Shell
POSIX-Style Command Line Shell Built in C
Summary
MYSH is a compact POSIX-style Unix command-line shell implemented in C. It provides a functional set of shell capabilities including command parsing, pipelines, input/output redirection, job control, and builtin commands.
The shell was designed to demonstrate core systems programming concepts such as process creation, signal handling, process groups, and file descriptor management. MYSH closely follows the behavior of traditional Unix shells, making it useful both as an educational tool and as a practical demonstration of low-level operating system concepts.
Key Features
- Command Parsing with tokenization and basic quoting support.
- Pipelines supporting multi-stage command chaining using the pipe operator
|. - Redirection support including
<,>,>>, numeric file descriptor redirection (2>file) and descriptor duplication (2>&1). - Job Control for background processes using
&with job tracking and control commands. - Built-in Commands including
cd,pwd,exit,echo,alias,unalias,type,help,export,set,unset, andhistory. - Execution Engine implementing fork/exec with correct process-group handling and foreground/background execution semantics.
Design & Architecture
MYSH follows a classic Unix shell architecture with modular components responsible for parsing commands, executing processes, and managing jobs.
- REPL Layer collects logical command lines and supports multi-line quoted inputs.
- Tokenizer & Parser convert raw input into a small command pipeline AST.
- Executor manages pipes, process creation, file descriptor redirection, and builtin execution.
- Job Control System tracks background jobs and safely reaps child processes using SIGCHLD.
Runtime Execution Flow
- Read input from the user and construct a logical command line.
- Tokenize the command while respecting quoted strings and escape sequences.
- Build pipeline structures where each stage represents a command execution unit.
- Create pipes, fork processes, configure redirections, and execute commands.
- Parent process manages foreground/background execution and updates the job table.
Core Data Model
Internally, MYSH relies on structured representations for commands and job management.
command_t— represents a pipeline stage containing arguments, redirection settings, and pipeline links.job_t— tracks job metadata including job ID, process group ID, command string, child PIDs, state, and exit status.
Built-in Commands
MYSH implements several built-in commands commonly found in Unix shells.
Commands that modify shell state such as cd or export are executed in the parent process,
while other builtins can run inside pipeline child processes when required.
Redirections are applied transparently so builtins behave consistently with external commands.
Example Commands
# basic command echo hello # pipeline example /usr/bin/printf "a\nb\nc\n" | /usr/bin/grep b | /usr/bin/wc -l # file redirection echo redir_test > /tmp/mysh_test1 # background job sleep 10 & jobs fg %1
Build & Execution
# build project make # start interactive shell ./bin/mysh # run scripted commands printf 'echo hello' | ./bin/mysh
Future Improvements
- Enhanced variable and word expansion.
- Persistent command history with line editing.
- Support for here-documents.
- Improved terminal foreground control using
tcsetpgrp.
Detailed documentation, build instructions, and technical examples are provided in the project documentation.