This project is part of the "Build Your Own Shell" Challenge by Codecrafters. Through this challenge, I’ve explored the fundamentals of Unix-like shell environments and implemented various features to bring this shell to life.
In this project, I've developed a basic shell that can handle built-in commands such as cd
, echo
, pwd
, type
, and also execute external programs found in the system’s PATH
. Here are some of the key capabilities and features I’ve added:
- Built-in Commands: Support for essential shell commands.
- External Program Execution: Running external binaries and scripts.
- Command History: Maintaining a session's command history and enabling auto-completion.
- Error Handling: Graceful handling of various error scenarios to ensure smooth operation.
-
Command History and Auto-completion:
- I set up command history using Python's
readline
module. - Implemented auto-completion for commands, making the shell more user-friendly.
- I set up command history using Python's
-
Built-in Commands:
echo
: Prints the provided arguments.pwd
: Displays the current working directory.cd
: Changes the working directory.type
: Checks if a command is built-in or an external executable.exit
: Exits the shell and saves the command history.
-
External Program Execution:
- My shell can search for and execute external programs, similar to how traditional shells operate.
-
Robust Error Handling:
- Implemented comprehensive error handling for file operations and command executions to provide meaningful feedback to the user.
- Python 3.11+.
-
Clone the Repository:
git clone https://github.com/catrilao/shell-python.git cd shell-python
-
Start the Shell:
./your_shell.sh
-
Try It Out: Once the shell is running, you can enter commands as you would in any terminal. Here are some commands you can try:
$ echo "Hello world!" $ pwd $ cd /some/directory $ type ls $ ls -la $ exit
echo Hello, World!
: PrintsHello, World!
.pwd
: Shows the current working directory.cd ~
: Changes to the home directory.type ls
: Checks ifls
is a built-in command or an external program.exit
: Exits the shell and saves the command history.
This project has been a fantastic learning experience. I’ve enjoyed diving into the internals of shell operations and translating those concepts into Python code. Here’s a brief overview of how I developed and tested each feature:
-
Initial Setup:
- Started by setting up a basic loop to read and execute commands.
- Implemented support for simple built-in commands.
-
Adding Functionality:
- Expanded support to handle external commands using Python’s
subprocess
module. - Enhanced the shell with command history and auto-completion.
- Expanded support to handle external commands using Python’s
-
Robust Error Handling:
- Added comprehensive error handling to provide clear feedback and maintain smooth operation.
-
Testing and Refinement:
- Manually tested each feature to ensure proper functionality.
- Refined command parsing and error messages for better user experience.
- Codecrafters: Thanks to Codecrafters for providing the "Build Your Own Shell" challenge. It has been instrumental in enhancing my skills in shell environments and Python programming.