A fully functional Unix-like shell written in Java. Just like bash or zsh, but built from scratch to teach you how shells really work.
ShellCraft is a working shell that can:
- β
Run commands (
ls,cat,grep, etc.) - β
Chain commands with pipes (
cat file.txt | grep "keyword") - β
Redirect output to files (
echo "hello" > file.txt) - β Remember command history
- β Auto-complete commands with TAB
- β Navigate history with arrow keys
git clone https://github.com/BackendArchitectX/ShellCraft.git
cd ShellCraft
mvn clean compilejava -cp target/classes com.pranay.cli.Main$ pwd
/current/directory
$ echo "Hello World"
Hello World
$ ls | grep .txt
file1.txt
file2.txtThat's it! You now have a working shell. π
These commands are built right into ShellCraft:
| Command | What It Does | Example |
|---|---|---|
echo |
Print text | echo Hello World |
pwd |
Show current folder | pwd |
cd |
Change folder | cd /tmp or cd ~ |
exit |
Close the shell | exit 0 |
history |
Show past commands | history or history 10 |
type |
Check if command exists | type echo |
$ ls -la
$ cat myfile.txt
$ grep "error" logfile.txt
$ python script.py$ ls -la | grep .java
$ cat file.txt | wc -l
$ ps aux | grep java# Save to file (overwrite)
$ echo "Hello" > output.txt
# Add to file (append)
$ echo "World" >> output.txt
# Save errors
$ command 2> errors.txt# Press the UP arrow key to go to previous commands
# Press the DOWN arrow key to go to next commands# Type part of a command and press TAB to auto-complete
$ ec[TAB] β echo
$ ex[TAB] β exit# See all past commands
$ history
# See last 10 commands
$ history 10
# Load history from file
$ history -r ~/.bash_history
# Save current history to file
$ history -w ~/.bash_history- You type a command β
$ echo hello - Shell parses it β Breaks into tokens:
["echo", "hello"] - Shell looks it up β Finds
echoin built-in commands - Shell runs it β Executes and prints:
hello
For external commands like ls:
- Shell searches PATH β Finds
/bin/ls - Shell spawns process β Runs the program
- Captures output β Pipes it back to you
For pipes like cat file.txt | grep keyword:
- Starts cat β Outputs file contents
- Pipes to grep β Receives input from cat
- Filters results β Shows only matching lines
src/main/java/com/pranay/
βββ cli/ β Main shell loop
βββ parser/ β Breaks input into commands
βββ command/ β Built-in command definitions
βββ exec/ β Runs commands and handles pipes
βββ env/ β Environment variables and PATH
βββ history/ β Command history management
βββ input/ β Tab completion & history navigation
- Java 8 or newer (check with
java -version) - Maven (for easy compilation)
- Linux/Mac/Unix (for
sttyterminal control)
# The command doesn't exist. Try:
$ type ls # Check if it's installed
$ which ls # Show full path to command# Make sure you compiled it first:
mvn clean compile
# Then run with correct classpath:
java -cp target/classes com.pranay.cli.Main# TAB completion needs a Unix/Linux terminal
# On Windows, use Windows Subsystem for Linux (WSL)$ cat file.txt | wc -l
42$ ls -la | grep .java
-rw-r--r-- 1 user group 5234 Main.java
-rw-r--r-- 1 user group 3421 Shell.java$ cat data.txt | grep "ERROR" > errors.txt
$ cat errors.txt
ERROR: Connection failed
ERROR: Timeout$ history -w ~/my_commands.txt
$ history -r ~/my_commands.txt
# Now you have your history saved!By exploring ShellCraft's code, you'll understand:
- How shells parse commands β Tokenizer & Parser
- How pipes work β PipelineCommand with Java threads
- How PATH works β PathResolver
- How to handle I/O redirection β Redirection handling
- How to build concurrent programs β Threading and piped streams
- How to design interactive CLIs β Input handling and completion
Perfect for system design interviews and understanding Unix internals.
- β Background jobs (
&) - β Environment variables (
$VAR) - β Wildcards/glob patterns (
*.txt) - β Complex conditionals (
if,case) - β Shell scripting (loops, functions)
These could be fun additions to try!
# Navigation
pwd # Show current directory
cd /path/to/dir # Change directory
cd ~ # Go to home directory
# View files
cat file.txt # Show file contents
ls -la # List files with details
grep "text" file # Search for text
# Create/Edit
echo "hello" > file # Create file with text
echo "world" >> file # Add more text
touch newfile.txt # Create empty file
# History
history # Show all commands
history 10 # Show last 10 commands
β / β # Navigate history with arrow keys
# Pipes & Redirection
cmd1 | cmd2 # Pipe output from cmd1 to cmd2
cmd > file # Save output to file
cmd >> file # Append output to file
cmd 2> errors # Save errors to file
# Completion
[TYPE_SOMETHING][TAB] # Auto-complete command
[TAB][TAB] # Show all options
# Exit
exit # Close the shell
exit 1 # Exit with error code- See all built-in commands:
history,pwd,echo,cd,exit,type - Check if command exists:
type ls - Learn Unix concepts: Try common commands like
ls,cat,grep,wc
Found a bug or have an idea? Feel free to:
- Fork the repository
- Create a branch with your feature
- Submit a pull request
Made with β€οΈ by Pranay Kadu
Learning resource: CodeCrafters - Build Your Own Shell
Happy shell scripting! π