explaingit

mvdan/sh

8,746GoAudience · developerComplexity · 2/5Setup · easy

TLDR

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.

Mindmap

mindmap
  root((repo))
    Core capabilities
      Parse shell scripts
      Format shell scripts
      Run shell scripts
    shfmt tool
      Command line formatter
      Editor plugins
      CI integration
    Supported shells
      POSIX shell
      Bash
      Zsh
      mksh
    Packaging
      Go library
      npm WASM package
      OS packages
Click or tap to explore — scroll the page freely

Code map

Detail Auto

An interactive map of this repo's files and how they connect — its source is parsed live in your browser. Click Visualize to build it.

filefunction / class

Things people build with this

USE CASE 1

Automatically format all shell scripts in your project to a consistent style using shfmt in a pre-commit hook or CI pipeline.

USE CASE 2

Parse a shell script into an abstract syntax tree inside a Go program so you can analyze or transform it programmatically.

USE CASE 3

Run shell scripts inside a Go application without spawning an external shell process using the interpreter library.

USE CASE 4

Use the sh-syntax npm package to format shell scripts inside a JavaScript or TypeScript project via WASM.

Tech stack

GoWASMJavaScript

Getting it running

Difficulty · easy Time to first run · 5min

shfmt is available via Homebrew, apt, and many other package managers, no compilation required for most users.

License information is not stated in the explanation.

In plain English

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.

Copy-paste prompts

Prompt 1
I want to run shfmt on all .sh files in my repo as part of a GitHub Actions CI check that fails if any file is not formatted. Show me the workflow and the shfmt command.
Prompt 2
Show me how to use the mvdan/sh Go library to parse a Bash script into an AST and walk it to find all function definitions.
Prompt 3
I want to run a small shell script inside my Go program using mvdan/sh's interpreter without calling os/exec. Show me a minimal working example.
Prompt 4
How do I install shfmt on macOS via Homebrew and configure it as a formatter for shell scripts in VS Code?
Open on GitHub → Explain another repo

← mvdan on gitmyhub — every repo by this author, as a profile.

Verify against the repo before relying on details.