Skip to content

Bhupi1306/codecrafters-shell-python

Repository files navigation

Custom Unix Shell (Python)

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.


Features

Built-in Commands

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 directory
  • pwd – print the current working directory
  • exit – terminate the shell session

Example:

cd /home/user
pwd
exit

External Command Execution

Commands 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.

Example:

ls
echo hello
cat file.txt

This behavior mirrors how standard Unix shells locate and execute programs.

Piping

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.

Example:

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.

Input / Output Redirection

The shell supports redirecting input and output streams to files.

Examples:

echo hello > file.txt
cat < file.txt

Supported 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.

Command History

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.

Example:

↑  previous command
↓  next command

Tab Completion

The 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

Example:

ec<Tab>

Auto-completes to:

echo

Tab completion is implemented using readline's completer functions.

Command Parsing

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

Example:

echo "hello world"

The parser converts raw user input into structured tokens that can be executed reliably.

About

Created my own shell in python

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors