A lightweight Unix-like command-line shell implemented in Python that replicates core behavior of traditional Unix shells such as bash.
This project was built as part of the Codecrafters "Build Your Own Shell" challenge, with the goal of understanding how real command-line shells work internally. The shell interprets user commands, executes programs, manages processes, and supports common shell features such as piping, input/output redirection, command history, and tab completion.
Building this project required working with process management, command parsing, file descriptors, and inter-process communication, which are fundamental concepts in operating systems.
The shell implements several built-in commands that are executed directly within the shell process rather than spawning a new system process.
Supported built-ins include:
cd– change the current working directorypwd– print the current working directoryexit– terminate the shell session
cd /home/user
pwd
exitCommands that are not built-ins are executed as external system processes.
The shell searches for the executable in the system PATH environment variable and launches it using Python's subprocess module.
ls
echo hello
cat file.txtThis behavior mirrors how standard Unix shells locate and execute programs.
The shell supports Unix-style pipelines, allowing multiple commands to be chained together so that the output of one command becomes the input of another.
ls | grep ".py"This feature is implemented using inter-process communication, where:
-
the stdout of one process is connected to
-
the stdin of the next process.
This allows complex command workflows to be built using simple programs.
The shell supports redirecting input and output streams to files.
Examples:
echo hello > file.txt
cat < file.txtSupported redirection operators:
- '>' Redirect command output to a file
- '<' Read command input from a file
This feature is implemented by manipulating file descriptors before executing commands.
The shell maintains a history of previously executed commands.
Features include:
-
storing executed commands
-
navigating history using arrow keys
-
quickly repeating previous commands
This functionality is implemented using the readline library, which provides command-line editing and history capabilities similar to bash.
↑ previous command
↓ next commandThe shell supports tab-based command auto-completion, allowing users to complete commands quickly.
Capabilities include:
-
completing commands available in the system PATH
-
suggesting commands when the user presses Tab
-
improving command-line productivity
ec<Tab>Auto-completes to:
echoTab completion is implemented using readline's completer functions.
User commands are parsed using Python's shlex module, which provides robust shell-style tokenization.
This allows the shell to correctly interpret:
-
quoted arguments
-
escaped characters
-
multi-argument commands
echo "hello world"The parser converts raw user input into structured tokens that can be executed reliably.