shfmt

Shell script formatter built on a Go parser for POSIX sh, Bash, Zsh and mksh. The same repo ships a syntax package, shell helpers and an interpreter.

installยท Go

shfmt formats shell scripts. It is the command-line front end of the mvdan/sh project, a Go library that parses, formats and interprets shell code. It supports POSIX Shell, Bash, Zsh and mksh. A typical invocation is shfmt -l -w script.sh, which lists files whose formatting differs and rewrites them in place. The default style is shown in the repository's canonical.sh, and a manpage documents the remaining flags.

The repository also provides Go packages. The syntax package parses scripts into a syntax tree that you can inspect and print back out the way shfmt does. The shell package has helpers to split a command line into arguments, quote them back like shlex, expand $VAR and ~, and glob with ** or match case-style patterns. The interp package runs shell scripts without a system shell, including on Windows, and its handlers control what scripts can execute and access. Because the library is pure Go, the interpreter cannot follow POSIX and Bash semantics exactly. For example, subshells run as goroutines, so real PIDs and file descriptors are not available.

The README lists known parser limits. Bash associative array indices should be quoted, since otherwise they are parsed as arithmetic expressions. The $(( versus (( ambiguity is not supported. export, let and declare are parsed as keywords. The formatter deliberately cannot be disabled for ranges of lines, and the project avoids adding formatting options.

Users are mainly developers and teams who want consistent shell formatting in editors, pre-commit hooks and CI. Documented integrations include plugins for VS Code, (Neo)Vim, Emacs, JetBrains IDEs and micro, a pre-commit hook, the sh-checker GitHub Action, a Prettier plugin, and mdformat-shfmt for scripts embedded in Markdown. Docker images are published for release tags, v3 and latest, and they contain only shfmt. For JavaScript use, the third-party npm package sh-syntax bundles the parser and formatter compiled to WASM.

Installation

# via Go
go install mvdan.cc/sh/v3/cmd/shfmt@latest

Alternatives & related tools

add a badge to your README

Maintain shfmt? Show that it's featured on trmn. The badge links back to this page.

Featured on trmn
Markdown
[![Featured on trmn](https://www.trmn.sh/badge/shfmt.svg)](https://www.trmn.sh/tools/shfmt?utm_source=badge)
HTML
<a href="https://www.trmn.sh/tools/shfmt?utm_source=badge"><img src="https://www.trmn.sh/badge/shfmt.svg" alt="Featured on trmn" height="20"></a>