What it does
You get a $ prompt and you can type commands at it. Some of them, like cd, pwd, echo, type and exit, the shell handles itself. Anything else gets looked up on your PATH and run as a real program. It understands single and double quotes, and sending output to a file with > or 1>.
Here’s an actual session, compiled and run on my machine:
$ echo 'hello from my shell' > notes.txt
$ cat notes.txt
hello from my shell
$ echo "in \"double\" quotes"
in "double" quotes
$ type git
git is C:\Program Files\Git\mingw64\bin\git.exe
$ git --version
git version 2.49.0.windows.1
$ ls
notes.txt
src
$ cd src
$ ls
Hello.java
$ type nope
nope: not found
$ exit
git and ls aren’t part of the shell. They’re real programs it found on PATH and started. The second ls lists src because the shell starts each program in its own current directory, which cd just changed.
Version one was one big class
The first version was one 516-line Main.java that I wrote in a single night. It worked, but echo, cat and running external programs each had their own copy-pasted quote parser. When I wrote the README I put that straight into the limitations section: it’s a single-class prototype, and it should be split into a shell loop, a parser and an executor.
The next evening I did that split. Now the main loop is short enough to read in one go:
while (running) {
System.out.print("$ ");
String command = sc.nextLine();
Parser p = new Parser(command);
if (CommandHelper.isBuiltin(p.getActualCommand())) {
running = new BuiltinCommands(p).execute();
} else {
new ExternalCommands(p).execute();
}
}
Read a line, parse it, hand it to whichever kind of command it is. Built-in and external commands both implement the same tiny interface, ICommand, with one method: boolean execute(). The return value means “keep the shell running”, which is how exit stops the loop without any special case in Main.
Quoting is harder than it looks
I thought parsing would be the easy part. It turned out to be most of the work, because the rules change with context:
- Inside single quotes, everything is literal. No escapes at all, and spaces are kept exactly as typed.
- Inside double quotes, a backslash only escapes four characters:
\,",$and`. Anywhere else it stays as a backslash.
if (c == '"') {
while (++i < original.length() && (c = original.charAt(i)) != '"') {
if (c == '\\' && i + 1 < original.length()) {
char next = original.charAt(i + 1);
// Only these are escaped inside double quotes
if (next == '\\' || next == '"' || next == '$' || next == '`') {
result.append(next);
i++;
continue;
}
}
result.append(c);
}
continue;
}
cat got its own small tokenizer. Once the quotes have been stripped, you can’t tell 'my file.txt' (one file) apart from my file.txt (two files), so cat reads the raw line again and splits it in a way that respects quotes.
Java can’t change its own directory
This one surprised me. A shell’s cd normally changes the working directory of the shell’s own process, and Java has no way to do that. So the shell keeps track of its own currentDirectory. Every path gets resolved against it with Paths.get(currentDirectory).resolve(p).normalize(), so .. and relative paths behave correctly. When an external program runs, ProcessBuilder.directory(...) starts it in the right place.
To find external programs, the shell goes through each folder in PATH and checks whether the file exists and is executable. On Windows it also tries the name with .exe on the end, which is why type git above finds git.exe.