A POSIX shell implementation written in Zig.
tsh implements the POSIX.1-2017 shell command language specification, including lexing, parsing, word expansion, and command execution. It links libc for POSIX process management and filesystem operations.
Requires Zig 0.15.2+. A Nix flake is provided for reproducible builds.
# Using Nix
nix develop .#tsh
zig build
# Or directly with Zig
zig build
zig build -Doptimize=ReleaseSafe
# Nix package
nix build# Interactive mode
zig build run
# Run a command
zig build run -- -c 'echo hello'
# Run a script
zig build run -- script.sh- Simple commands with argument lists
- Pipelines (
cmd1 | cmd2 | cmd3) - AND/OR lists (
cmd1 && cmd2,cmd1 || cmd2) - I/O redirections (
>,>>,<,2>,>&) - Subshells for pipeline stages
if/elif/else/fiwhile/untilloopsfor name in words; do ...; donebreakandcontinuewith nesting depth
- Tilde expansion (
~,~/path) - Parameter expansion (
$VAR,${VAR},${VAR:-default},${VAR:+alt},${VAR:=assign},${VAR:?error},${#VAR},${VAR%pat},${VAR%%pat},${VAR#pat},${VAR##pat}) - Command substitution (
$(cmd)) - Special parameters (
$?,$#,$@,$*,$0,$$) - Field splitting on IFS (custom IFS support)
- Pathname expansion / globbing (
*,?,[...]) - Quote removal (single quotes, double quotes, backslash escaping)
cd, exit, export, pwd, test / [, unset
- Arithmetic expansion (
$((expr))) - Here-documents (
<<) - Backtick command substitution (
`cmd`) casestatements- Functions
- Job control
- Signal handling
set,readonly,eval,exec,./source,read,trap
# Run all tests
zig build test
# Run tests for a specific module
zig test src/lexer.zig --dep tsh -Mroot=src/root.zig
zig test src/expand.zig --dep tsh -Mroot=src/root.zig# Dump token stream
zig build run -- --dump-tokens < script.sh
# Dump AST
zig build run -- --dump-ast < script.sh