Skip to content
rayko04Public

About

A minimal Unix Shell written in C.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Latest commit

 

History

7 Commits

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 

Repository files navigation

MiniShell

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.

Features

  • Interactive shell prompt
  • Execute external programs using fork() and execvp()
  • Built-in commands
    • cd
    • pwd
    • exit
  • Input redirection (<)
  • Output redirection (>)
  • Output append redirection (>>)
  • Arbitrary-length pipelines (|)
  • Multiple command parsing
  • Basic syntax validation
  • Proper child process management using wait()

Supported Syntax

Execute Programs

ls
ls -l
echo Hello World

Built-in Commands

pwd
cd ..
cd
exit

Output Redirection

echo Hello > file.txt
ls > output.txt

Append Redirection

echo First > file.txt
echo Second >> file.txt

Input Redirection

cat < file.txt
sort < numbers.txt

Pipelines

ls | wc
cat file.txt | grep hello
ls -l | grep ".c" | wc

Mixed Operations

cat < input.txt | grep hello | wc > result.txt

Project Structure

.
├── 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

How It Works

Single Command

Read Input
      │
      ▼
 Parse Command
      │
      ▼
 Built-in?
   │       │
 Yes      No
 │         │
Run     fork()
 │         │
 │      execvp()
 ▼         ▼
Return   wait()

Pipeline

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.


Concepts Demonstrated

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

Error Handling

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

Limitations

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

Building

Compile using GCC:

gcc -Wall -Wextra -std=c11 shell.c -o minishell

Or with debugging information:

gcc -g -Wall -Wextra shell.c -o minishell

Running

./minishell

Example session:

myshell> pwd
/home/user

myshell> ls | grep ".c"
shell.c

myshell> echo Hello > output.txt

myshell> cat < output.txt
Hello

myshell> exit

Learning Objectives

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

Future Improvements

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

License

This project was created for educational purposes.

About

A minimal Unix Shell written in C.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages