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.
-
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
.
- Executes programs available in the system's
-
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>>
).
- Supports output redirection (
-
Error Handling:
- Gracefully handles invalid commands and displays appropriate error messages.
- Node.js: Ensure you have Node.js (version 21 or higher) installed on your system.
-
Clone the repository:
git clone https://github.com/your-username/build-your-own-shell.git cd build-your-own-shell
-
Install dependencies
npm install
To start the shell, run:
./your_program.sh
You will see a prompt ($ ) where you can enter 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
You can run any executable available in your system's PATH. For example: $ ls -l $ node --version
Press to autocomplete commands or filenames. If multiple matches exist, press twice to display all matches.
-
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
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).
- 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: