Skip to content

About

POSIX-compliant shell written from scratch in Java: command parsing, quoting, redirection, pipelines, and builtins, with no shell libraries.

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Latest commit

 

History

49 Commits

Folders and files

Repository files navigation

A POSIX shell in Java

A working shell, written from scratch in one file of Java with no shell library behind it. Started from the CodeCrafters "Build Your Own Shell" challenge and carried past it into job control.

mvn -q package
./your_program.sh

What it does

Builtins: echo, exit, type, pwd, cd, jobs. cd handles ~, absolute paths and relative paths, and reports a missing directory rather than silently staying put.

External commands resolved by walking PATH in order, so type can tell you whether a name is a builtin or the file it would actually run.

Quoting handled by a hand-written tokeniser, not String.split. Single quotes take everything literally. Double quotes keep backslash escapes for " and \ and leave a backslash literal in front of anything else. Outside quotes, a backslash escapes the next character. This is the part of a shell that looks easy and is not; most of the parser is this.

Redirection: >, 1>, >>, 1>> for stdout, 2> and 2>> for stderr, including on a command inside a pipeline.

Pipelines of any length, each stage in its own process, with the file descriptors wired between them.

Background jobs with a trailing &. Running jobs get an id, jobs lists them with the usual + and - markers for the current and previous job, and finished jobs are reaped and reported instead of hanging around as zombies.

Layout

src/main/java/Main.java, 863 lines. The interesting parts are parseInput for tokenising, runPipeline for process wiring, and the job table at the top.

About

POSIX-compliant shell written from scratch in Java: command parsing, quoting, redirection, pipelines, and builtins, with no shell libraries.

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages