sh
A shell parser, formatter, and interpreter with bash and zsh support; includes shfmt
A Go library that parses, formats, and runs shell scripts, best known for shfmt, a command-line formatter for Bash and POSIX shell that works like gofmt and is packaged for most Linux distros and editors.
This Go library does three things with shell scripts: it parses them into a structured representation, it formats them to a consistent style, and it can interpret (run) them directly in a Go program. It supports POSIX shell syntax, Bash, Zsh, and mksh.
The most widely used piece is shfmt, a command-line tool for automatically formatting shell scripts, similar to how gofmt works for Go code. You run it against a script file and it rewrites the file with consistent indentation, spacing, and structure. It is packaged for Alpine, Arch, Debian, Fedora, Homebrew, NixOS, Snapcraft, and several other systems, and Docker images are also published. Editor plugins exist for VS Code, Vim, Neovim, Emacs, JetBrains IDEs, and Sublime Text.
The parser is available as a Go library for programs that need to read and inspect shell scripts. It builds an abstract syntax tree you can walk and analyze. The interpreter library lets you run shell scripts inside a Go application without calling an external shell binary.
A JavaScript version is available as an npm package called sh-syntax, which bundles this library compiled to WASM, so it can be used in JavaScript projects.
The README notes a few limitations. The library is written in pure Go, which means some Bash behaviors that depend on OS-level features like process forking are approximated rather than replicated exactly. The formatter also intentionally avoids per-file or per-region disable comments, and keeps formatting options minimal on purpose, since the value of a formatter comes from consistency rather than per-developer preference.
Where it fits
- Automatically format all shell scripts in your project to a consistent style using shfmt in a pre-commit hook or CI pipeline.
- Parse a shell script into an abstract syntax tree inside a Go program so you can analyze or transform it programmatically.
- Run shell scripts inside a Go application without spawning an external shell process using the interpreter library.
- Use the sh-syntax npm package to format shell scripts inside a JavaScript or TypeScript project via WASM.