Skip to content

A custom Unix-like shell written in C that executes commands in separate processes, supports background execution, internal commands, and a history feature.

Notifications You must be signed in to change notification settings

manavbansal1/Simple-Shell

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

32 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Simple Shell

Overview

Simple Shell is a custom command-line shell that I implemented in C, showcasing my understanding of process management, signals, and Unix system programming. This shell supports executing commands in both foreground and background modes, includes several built-in commands, and maintains a history of the most recent 10 commands.

While the project’s specifications were based on SFU CMPT 201 Assignment 8, I designed and implemented the full shell functionality, including process handling, signal management, and command parsing.

Features

Execute Commands

  • Runs user commands in foreground or background depending on the presence of &.
  • Implements proper error handling for invalid commands or system call failures.
  • Cleans up zombie processes for background commands.

Internal Commands

  • exit: Exits the shell.
  • pwd: Prints the current working directory.
  • cd: Changes the working directory with support for:
    • No argument → switches to home directory
    • ~ → home directory shortcut
    • - → previous directory
  • help: Lists internal commands or provides help for a specific command.

History Feature

  • Maintains the 10 most recent commands with indices.
  • history: Displays command history in descending order.
  • !!: Re-runs the last command.
  • !n: Re-runs the command with index n.
  • Tracks background commands in history as well.

Signal Handling

  • Handles SIGINT (Ctrl-C) gracefully, without terminating the shell.
  • Displays help information when SIGINT is received.

Project Structure

simple_shell/
├── CMakeLists.txt         # Build configuration
├── include/               # Header files
│   └── shell.h
├── src/                   # Source code files
│   ├── main.c
│   ├── commands.c
│   ├── history.c
│   └── utils.c
└── gtest/                 # Test cases (Google Test)

Build and Run

Requirements: clang, clang++, CMake

# Set compilers
export CC=$(which clang)
export CXX=$(which clang++)

# Build the project
cmake -S . -B build
cmake --build build

# Run the shell
./build/shell

Usage Examples

  • Foreground execution:
/home/user$ ls -la
  • Background execution:
/home/user$ sleep 10 &
  • Change directory:
/home/user$ cd ~/Documents
  • Display history:
/home/user$ history
  • Re-run a previous command:
/home/user$ !3

Technical Highlights

  • Implemented using fork(), execvp(), and waitpid() for robust process management.
  • Uses read() and write() for safe input/output handling with signals.
  • Handles zombie processes and ensures the shell remains responsive for new commands.
  • Built-in commands are executed without spawning new processes.

Future Enhancements

  • Support for input/output redirection (>).
  • Support for command pipelines (|).
  • Extendable history storage beyond the last 10 commands.

Acknowledgment

The project’s requirements and grading specifications were adapted from SFU CMPT 201 Assignment 8. All code, design decisions, and implementation were developed independently.

About

A custom Unix-like shell written in C that executes commands in separate processes, supports background execution, internal commands, and a history feature.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Contributors 2

  •  
  •