A minimal Unix shell written in C for learning Unix process management, file descriptors, and shell internals. The project was built from scratch without using system() or existing shell implementations, focusing on understanding how shells work at the operating system level.
- Interactive shell prompt
- Execute external programs using
fork()andexecvp() - Built-in commands
cdpwdexit
- Input redirection (
<) - Output redirection (
>) - Output append redirection (
>>) - Arbitrary-length pipelines (
|) - Multiple command parsing
- Basic syntax validation
- Proper child process management using
wait()
ls
ls -l
echo Hello Worldpwd
cd ..
cd
exitecho Hello > file.txt
ls > output.txtecho First > file.txt
echo Second >> file.txtcat < file.txt
sort < numbers.txtls | wc
cat file.txt | grep hello
ls -l | grep ".c" | wccat < input.txt | grep hello | wc > result.txt.
├── shell.c
└── README.md
The shell is organized into several logical components:
| Function | Responsibility |
|---|---|
readInput() |
Reads user input using getline() |
parse() |
Parses commands, arguments, redirections, and pipelines |
readFromFile() |
Handles input redirection |
writeToFile() |
Handles output and append redirection |
runBuiltIn() |
Executes built-in commands |
execSingle() |
Executes a single command |
execPipe() |
Executes pipelines of arbitrary length |
Read Input
│
▼
Parse Command
│
▼
Built-in?
│ │
Yes No
│ │
Run fork()
│ │
│ execvp()
▼ ▼
Return wait()
Command 1
│
pipe()
│
Command 2
│
pipe()
│
Command 3
│
...
│
Command N
Each command executes in its own child process while the parent creates pipes and waits for all children to finish.
This project explores several fundamental Unix and Operating Systems concepts:
- Process creation (
fork) - Program execution (
execvp) - Parent-child synchronization (
wait) - Pipes (
pipe) - File descriptor duplication (
dup2) - Standard input/output redirection
- File descriptor management
- Parsing command-line input
- Built-in command handling
- Unix process model
The shell detects several common syntax errors, including:
- Missing filenames after redirection operators
- Consecutive operators
- Empty pipeline stages
- Multiple input redirections
- Multiple output redirections
- Excessive number of pipeline commands
This is an educational shell and intentionally omits many features found in production shells.
Currently unsupported:
- Environment variable expansion (
$HOME) - Wildcards (
*,?) - Command history
- Background execution (
&) - Job control
- Quoting and escaping
- Logical operators (
&&,||) - Command substitution
- Shell scripting
- Aliases
- Signal handling
Compile using GCC:
gcc -Wall -Wextra -std=c11 shell.c -o minishellOr with debugging information:
gcc -g -Wall -Wextra shell.c -o minishell./minishellExample session:
myshell> pwd
/home/user
myshell> ls | grep ".c"
shell.c
myshell> echo Hello > output.txt
myshell> cat < output.txt
Hello
myshell> exit
The goal of this project was to understand how Unix shells operate internally by implementing core functionality from scratch rather than relying on library wrappers or existing shell implementations.
Key topics explored include:
- Process management
- Inter-process communication
- File descriptors
- I/O redirection
- Command parsing
- Pipeline construction
- Built-in command execution
Potential future enhancements include:
- Dynamic command allocation
- Environment variable expansion
- Wildcard expansion using
glob() - Background jobs (
&) - Command history
- Signal handling (
Ctrl+C,Ctrl+Z) - Quote parsing
- Logical operators (
&&,||) - Job control (
fg,bg) - Shell scripting support
- Auto-completion
This project was created for educational purposes.