A feature-rich custom shell implementation in Java that provides Unix-like shell functionality with support for built-in commands, pipelines, command history, and more.
exit- Exit the shellecho [text]- Print text to standard outputpwd- Print current working directorycd [directory]- Change directory (supports~, relative and absolute paths)type [command]- Check if a command is a builtin or show its executable pathcat [files...]- Display contents of one or more fileshistory [n]- Display command history
- 🔗 Pipeline Support - Chain commands using
|operator - 📝 Persistent History - Automatic history save/load using
HISTFILEenvironment variable - ⬆️⬇️ Arrow Key Navigation - Browse command history (Unix/Linux/Mac)
- 🎯 Quote Handling - Support for single quotes, double quotes, and backslash escaping
- 🚀 External Command Execution - Run any program available in your system's PATH
- 💾 History Management - Read, write, and append history to custom files
- Java Development Kit (JDK) 8 or higher
- Java compiler (
javac)
-
Clone the repository
git clone https://github.com/smritii73/BuildShellUsingJava.git cd BuildShellUsingJava -
Compile the code
javac Main.java
-
Run the shell
java Main
$ pwd
/home/user/projects
$ echo Hello World
Hello World
$ cd ~
$ pwd
/home/userChain multiple commands together:
$ echo "Hello World" | cat
Hello World
$ pwd | cat
/current/directory# View all history
$ history
# View last 5 commands
$ history 5
# Save history to file
$ history -w commands.txt
# Append new commands to file
$ history -a session_log.txt
# Load history from file
$ history -r previous_commands.txt$ cat file1.txt
Contents of file1
$ cat file1.txt file2.txt file3.txt
Contents of file1
Contents of file2
Contents of file3$ echo 'single quotes preserve everything'
single quotes preserve everything
$ echo "double quotes allow \"escapes\""
double quotes allow "escapes"
$ echo backslash\ works
backslash worksSet the HISTFILE environment variable before starting the shell:
Unix/Linux/Mac:
export HISTFILE=~/.myshell_history
java MainWindows (PowerShell):
$env:HISTFILE="$HOME\.myshell_history"
java MainThe shell will automatically:
- Load history from
HISTFILEon startup - Save history to
HISTFILEon exit
| Command | Description |
|---|---|
history |
Display all commands with line numbers |
history N |
Display last N commands |
history -r file |
Read/load history from file |
history -w file |
Write all history to file (overwrite) |
history -a file |
Append new session commands to file |
| Shortcut | Action |
|---|---|
↑ (Up Arrow) |
Previous command in history |
↓ (Down Arrow) |
Next command in history |
Backspace |
Delete character |
Ctrl+C |
Cancel current line |
Ctrl+D |
Exit shell (when line is empty) |
Enter |
Execute command |
Note: Arrow key navigation works on Unix/Linux/Mac systems. On Windows, it falls back to simple line reading.
- Built-in Commands: Implemented directly in Java
- External Commands: Executed via
ProcessBuilder - Pipeline Execution: Mixed threading model supporting both built-ins and external commands
- Input Parsing: Custom parser handling quotes and escapes
- Single quotes (
'): Preserve all characters literally - Double quotes (
"): Allow escape sequences for$,`,",\, and newline - Backslash (
\): Escape the next character
- Supports multiple commands in a single pipeline
- Uses
PipedInputStreamandPipedOutputStreamfor inter-command communication - Threads handle concurrent execution of pipeline stages
- Both built-in and external commands can be chained
| Feature | Windows | Unix/Linux/Mac |
|---|---|---|
| Basic commands | ✅ | ✅ |
| Pipelines | ✅ | ✅ |
| Arrow key history | ❌ | ✅ |
| External commands | ✅ | ✅ |
| History file | ✅ | ✅ |
This shell does not support:
- Input/output redirection (
>,<,>>) - Background jobs (
&) - Environment variable expansion (
$VAR) - Command substitution (
$(command)) - Wildcards/globbing (
*.txt) - Logical operators (
;,&&,||)
Contributions are welcome! Feel free to:
- Report bugs
- Suggest new features
- Submit pull requests
This project is open source and available under the MIT License.
Smriti - @smritii73 Vaishnavi - @vaishnavidhule Jaykit - @Jaykit1907 Nikshit - @143Nikshit