How mvdan/sh works
This repository is a Go shell toolkit: it parses and formats POSIX/Bash-family scripts, provides shell-semantic string helpers, and interprets scripts without requiring a system shell. The main execution workflow is gosh input → syntax parsing → interpreter execution, with configurable handlers for command and filesystem access. shfmt is a separate formatting workflow through the syntax package. The README also documents globbing, matching, and expansion helpers. Relationships are limited to those shown in excerpts, indexed imports, or README statements; some internal details are not sampled.
Main entry points: runner.go Interpreter runner · parser.go Shell parser · syntax Syntax API · param.go Parameter expansion
Components 23
Shell syntax
- Syntax API
syntax - Shell lexer
syntax/lexer.go - Shell parser
syntax/parser.go - Syntax tree
syntax/nodes.go - Shell printer
syntax/printer.go - Typed JSON
syntax/typedjson/json.go
Shell helpers
- Word expansion
expand/expand.go - Parameter expansion
expand/param.go - Arithmetic expansion
expand/arith.go - Pattern matching
pattern/pattern.go - Split and quote
shell/split.go - String expansion
shell/expand.go - Shell globbing
shell/glob.go - Case matching
shell/match.go
Script runtime
- Interpreter runner
interp/runner.go - Runtime API
interp/api.go - Shell builtins
interp/builtin.go - Execution handlers
interp/handler.go - Runtime variables
interp/vars.go - Core utilities
x/coreutils/coreutils.go
Command-line tools
- gosh runner
cmd/gosh/main.go - shfmt formatter
cmd/shfmt/main.go
Other components
- Script user
Connections 25
- Script user → gosh runner: provides script
- gosh runner → Shell parser: parses input
cmd/gosh/main.go - Shell parser → Syntax tree: builds tree
- gosh runner → Interpreter runner: runs program
cmd/gosh/main.go - Interpreter runner → Syntax tree: executes tree
interp/runner.go - Interpreter runner → Word expansion: expands words
interp/runner.go - Word expansion → Parameter expansion: expands parameters
expand/expand.go - Parameter expansion → Arithmetic expansion: evaluates arithmetic
expand/param.go - Parameter expansion → Pattern matching: uses patterns
expand/param.go - Runtime API → Interpreter runner: configures
interp/api.go - Interpreter runner → Shell builtins: dispatches builtins
interp/runner.go - Interpreter runner → Execution handlers: uses handlers
interp/handler.go - Interpreter runner → Runtime variables: accesses variables
interp/runner.go - Core utilities → Runtime API: integrates with
x/coreutils/coreutils.go - Script user → shfmt formatter: requests formatting
- shfmt formatter → Syntax API: formats scripts
cmd/shfmt/main.go - Syntax API → Shell parser: parses scripts
- Syntax API → Shell printer: prints scripts
- shfmt formatter → Typed JSON: uses JSON trees
cmd/shfmt/main.go - Script user → Split and quote: uses helpers
- Split and quote → Shell parser: parses words
shell/split.go - String expansion → Word expansion: uses expansion
shell/expand.go - Script user → String expansion: expands strings
- Script user → Shell globbing: globs paths
- Script user → Case matching: matches patterns