Skip to content

A custom POSIX-compliant shell built in JavaScript, featuring support for built-in commands (cd, pwd, echo, etc.), external executables, tab completion, redirection, and error handling. Learn how shells work under the hood while exploring command parsing, REPLs, and more.

Notifications You must be signed in to change notification settings

daviidy/codecrafters-shell-javascript

Repository files navigation

Build Your Own Shell

This project is a custom implementation of a POSIX-compliant shell built in JavaScript. It supports executing external commands, built-in commands, and features like tab completion, command parsing, and redirection.

Features

  • Built-in Commands:

    • cd: Change the current working directory.
    • pwd: Print the current working directory.
    • echo: Print arguments to standard output.
    • exit: Exit the shell.
    • type: Display information about a command (e.g., whether it's built-in or external).
  • External Commands:

    • Executes programs available in the system's PATH.
  • Tab Completion:

    • Autocompletes built-in commands and external executables.
    • Handles cases where multiple matches exist, completing to the longest common prefix or displaying all matches.
  • Redirection:

    • Supports output redirection (>, >>) and error redirection (2>, 2>>).
  • Error Handling:

    • Gracefully handles invalid commands and displays appropriate error messages.

Getting Started

Prerequisites

  • Node.js: Ensure you have Node.js (version 21 or higher) installed on your system.

Installation

  1. Clone the repository:

    git clone https://github.com/your-username/build-your-own-shell.git
    cd build-your-own-shell
  2. Install dependencies

    npm install

Running the Shell

To start the shell, run:

./your_program.sh

You will see a prompt ($ ) where you can enter commands.

Usage

Built-in Commands

  • cd : Change the current working directory.
$ cd /path/to/directory
  • pwd: Print the current working directory.
$ pwd
  • echo : Print text to standard output.
$ echo Hello, World!
  • exit: Exit the shell.
$ exit
  • type : Display information about a command.
$ type echo
echo is a shell builtin

External Commands

You can run any executable available in your system's PATH. For example: $ ls -l $ node --version

Tab Completion

Press to autocomplete commands or filenames. If multiple matches exist, press twice to display all matches.

Redirection

  • Redirect standard output to a file: $ echo "Hello" > output.txt

  • Append to a file: $ echo "World" >> output.txt

  • Redirect standard error: $ ls nonexistentfile 2> error.log

  • Append standard error: $ ls nonexistentfile 2>> error.log

Development

Project Structure

  • app/main.js: Entry point for the shell.
  • app/InputHandler.js: Handles user input and tab completion.
  • app/OutputHandler.js: Handles output and redirection.
  • app/CommandRegistry.js: Manages built-in and external commands.
  • app/<CommandName>.js: Implementations of built-in commands (e.g., CdCommand, PwdCommand).

Adding a New Built-in Command

  • Create a new file in the app directory (e.g., MyCommand.js).
  • Extend the Command class and implement the execute method.
  • Register the command in main.js:

About

A custom POSIX-compliant shell built in JavaScript, featuring support for built-in commands (cd, pwd, echo, etc.), external executables, tab completion, redirection, and error handling. Learn how shells work under the hood while exploring command parsing, REPLs, and more.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published