Skip to content

tommoa/tsh

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

69 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

tsh

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.

Building

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

Usage

# Interactive mode
zig build run

# Run a command
zig build run -- -c 'echo hello'

# Run a script
zig build run -- script.sh

Features

Command Execution

  • Simple commands with argument lists
  • Pipelines (cmd1 | cmd2 | cmd3)
  • AND/OR lists (cmd1 && cmd2, cmd1 || cmd2)
  • I/O redirections (>, >>, <, 2>, >&)
  • Subshells for pipeline stages

Control Flow

  • if / elif / else / fi
  • while / until loops
  • for name in words; do ...; done
  • break and continue with nesting depth

Word Expansion (POSIX 2.6)

  • 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)

Builtins

cd, exit, export, pwd, test / [, unset

Not Yet Implemented

  • Arithmetic expansion ($((expr)))
  • Here-documents (<<)
  • Backtick command substitution (`cmd`)
  • case statements
  • Functions
  • Job control
  • Signal handling
  • set, readonly, eval, exec, ./source, read, trap

Testing

# 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

Debug Tools

# Dump token stream
zig build run -- --dump-tokens < script.sh

# Dump AST
zig build run -- --dump-ast < script.sh

About

A POSIX shell, written in zig

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published