Skip to content

SurakshanHN/Minimal-UNIX-Shell

Repository files navigation

Minimal UNIX Shell 🐚

A modular, POSIX-compliant shell implemented in C, following the RPEL (Read-Parse-Execute-Loop) architecture. This project serves as a foundation for understanding OS-level process management, memory handling, and terminal interaction.

Architecture: The RPEL Model

The shell is designed with modularity in mind, separating concerns into individual components:

  1. Read:
    • read_line.c: Handles interactive input using getline.
    • read_stream.c: Handles piped/scripted input using a character-by-character buffer.
  2. Parse:
    • split_line.c: Tokenizes input into commands and arguments using strtok. Supports comments (#).
  3. Execute:
    • execute_args.c: A dispatcher that checks for built-in commands before attempting to launch external programs.
    • builtins.c: Internal implementations for cd, env, help, and exit.
    • new_process.c: Creates child processes using fork(), replaces them with execvp(), and monitors status with waitpid().
  4. Loop:
    • shell_interactive.c: Manages the prompt and life cycle for terminal usage.
    • shell_no_interactive.c: Manages execution flow for non-terminal input.

Installation & Setup

This shell relies on POSIX system calls (fork, exec, wait). Follow the instructions below based on your Operating System.

🐧 Linux (Recommended)

  1. Install Build Tools:
    sudo apt update
    sudo apt install build-essential
  2. Compile and Run:
    make hsh
    ./hsh

🍎 macOS

  1. Install Command Line Tools:
    xcode-select --install
  2. Compile and Run:
    make hsh
    ./hsh

🪟 Windows

Native Windows (Command Prompt/PowerShell) does not support the fork() system call. To run this shell, you MUST use a POSIX-compatible layer.

Option A: WSL (Windows Subsystem for Linux) - Highly Recommended

  1. Open PowerShell as Administrator and run: wsl --install.
  2. Restart your PC and open the Ubuntu terminal.
  3. Follow the Linux instructions above.

Option B: MSYS2 / Cygwin

  1. Install MSYS2.
  2. Open the MSYS2 MINGW64 terminal.
  3. Install the toolchain: pacman -S mingw-w64-x86_64-toolchain make.
  4. Compile using make hsh.

Usage

Interactive Mode

Run the shell directly to see the prompt:

./hsh
simple_prompt$ ls -l
simple_prompt$ cd ..
simple_prompt$ help

Non-Interactive Mode

Pipe commands into the executable:

echo "ls -l" | ./hsh

Cleanup

To remove compiled object files and the executable:

make clean

Limitations

  • Environment: Does not run natively on Windows cmd.exe.
  • Features: Does not currently support piping (|), redirection (>, <), or globbing (*).
  • Expansion: Environment variable expansion (e.g., $HOME) is handled by the system's execvp but not by the parser itself for argument building.

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors