A minimal shell environment written in Python. This script provides basic shell functionalities, including built-in commands and the ability to execute external programs.
-
Built-in commands:
echo
: Prints the given arguments to the standard output.type
: Displays information about a command (e.g., if it is a shell built-in or an external program).pwd
: Prints the current working directory.cd
: Changes the current directory.cat
: Prints the content of files to the standard output.exit 0
: Exits the shell.
-
Executes external commands available in the system's PATH.
-
Redirecting/appending of command outputs and or errors into files.
-
Robust error handling for invalid commands, missing files, and permission issues.
-
Graceful handling of keyboard interruptions (
Ctrl+C
).
The shell supports the following commands:
-
echo [arguments...]
- Prints the arguments to the terminal.
- Example:
$ echo Hello, World! Hello, World!
-
type [command]
- Displays information about a command (e.g., whether it is a shell built-in or an external program).
- Example:
$ type echo echo is a shell builtin
-
pwd
- Prints the current working directory.
- Example:
$ pwd /home/user
-
cd [directory]
- Changes the current working directory. Use
cd ~
to navigate to the home directory. - Example:
$ cd /path/to/directory $ pwd /path/to/directory
- Changes the current working directory. Use
-
cat [file...]
- Prints the contents of one or more files to the terminal.
- Example:
$ cat file1.txt file2.txt [contents of file1.txt] [contents of file2.txt]
-
exit 0
- Exits the shell.
- You can also run any external commands available in the system PATH:
$ ls -l [output of `ls -l`]
The shell supports standard output and error redirection for commands. You can redirect the output of both built-in and external commands to files.
-
Overwrite existing file (
>
or1>
):$ echo "Hello, World!" > output.txt
This command will write "Hello, World!" to
output.txt
, overwriting any existing content. -
Append to existing file (
>>
or1>>
):$ echo "Hello, again!" >> output.txt
This command will append "Hello, again!" to
output.txt
.
-
Overwrite existing file (
2>
):$ cat non_existent_file 2> error.log
This command will redirect the error message to
error.log
, overwriting any existing content. -
Append to existing file (
2>>
):$ cat non_existent_file 2>> error.log
This command will append the error message to
error.log
.
- Redirect both stdout and stderr to separate files:
This redirects standard output to
$ command > output.log 2> error.log
output.log
and standard error toerror.log
.
-
Save command output:
$ pwd > current_directory.txt
-
Log errors:
$ cat missing_file.txt 2> error_log.txt
-
Append multiple outputs:
$ echo "Line 1" >> log.txt $ echo "Line 2" >> log.txt
-
Save both output and errors:
$ ls /some/path > output.log 2> errors.log
Note: The shell creates directories automatically if they don't exist when redirecting output to a file in a non-existent directory.
To install and run the Basic Shell Emulator, follow these steps:
-
Clone the repository to your local machine using Git:
git clone https://github.com/LogicalGuy77/py-shell.git cd py-shell
-
Once you turn on WSL ( if on windows ), you can run the shell emulator using the provided
py_shell.sh
script. This script will execute the Python code:./py_shell.sh
-
If you don't want to use the
py_shell.sh
script, you can run the Python script directly by using:cd .\app\ python main.py
-
Platform Compatibility: The shell should work on Linux and macOS. MacOS users may need to adjust the script execution process to ensure compatibility with their environment.
-
Permissions: Ensure that the mini-shell.sh script is executable. If not, you can make it executable using:
chmod +x py_shell.sh
This project was built as part of the Codecrafters challenge, with all implementation done independently.