Formatters
What is shfmt? An introduction to mvdan's shell script formatter
shfmt is a shell script formatter that is part of the mvdan/sh project. The project is a shell parser, formatter and interpreter supporting POSIX Shell, Bash, Zsh and mksh. shfmt formats shell programs and has a documented default style.
- Tool
- shfmt
- Made by
- mvdan
- Official site
- github.com/mvdan/sh
- Category
- Formatters
Who makes shfmt
shfmt is made by mvdan as part of the mvdan/sh project, which is hosted on GitHub. The official website is linked below and points to that repository.
The repository covers more than the formatter. It is a library and tool set for shell, and shfmt is the formatting part of it.
Official website: github.com/mvdan/sh
What shfmt is for
shfmt is for formatting shell scripts so they follow a consistent layout. It formats shell programs and has a documented default style, so the output is predictable.
The maintainers prioritize formatting consistency, especially for teams, over personal preferences. In line with that, the project deliberately limits the number of formatting options. Formatting also cannot be disabled for ranges of lines, so a file is formatted as a whole.
Who shfmt is for
The stated audience is shell script authors and teams of developers. For individual authors, shfmt provides a ready-made style without configuration choices to make. For teams, the small option set and the emphasis on consistency mean that scripts look the same regardless of who wrote them.
The surrounding packages also make the project relevant to developers who build tools that handle shell code, since the parser and helper packages can be used from Go.
Notable capabilities
Beyond formatting, the mvdan/sh project exposes its parser and interpreter as packages, and it is distributed in several ways. These are the capabilities named in the verified facts.
- The syntax package parses scripts into a syntax tree that can be inspected and formatted.
- The shell package offers helpers to split, quote and expand command lines and glob patterns.
- The interp package runs shell scripts without a system shell, including on Windows.
- shfmt can convert shell to and from a JSON syntax tree.
- The library is written in pure Go.
- Packages are available across many operating systems and package managers, and release tags are published as Docker images.
- A third-party npm package, sh-syntax, bundles the parser and formatter compiled to WASM.
- Editor integrations that wrap shfmt exist, and a GitHub Action, sh-checker, performs static analysis for shell scripts using shfmt.
How shfmt fits formatters
Formatters standardize how code is laid out without changing its behavior. shfmt covers this for shell, and it supports POSIX Shell, Bash, Zsh and mksh through the project's parser.
Its design choice is to limit options. That separates it from formatters built around extensive configuration, and it suits teams that want the style decided once and applied the same way everywhere.
For Linthelm's wider niche of linters, static analysis and code quality, shfmt handles the formatting side of shell code. The sh-checker GitHub Action shows how it can be combined with static analysis for shell scripts, and the syntax tree that shfmt builds is available for inspection through the syntax package.
Frequently asked questions
What is shfmt used for?
shfmt is used to format shell programs. It has a documented default style and deliberately limits the number of formatting options.
Which shells does shfmt support?
The mvdan/sh project supports POSIX Shell, Bash, Zsh and mksh. shfmt is the formatter in that project.
Who develops shfmt?
shfmt is developed by mvdan as part of the mvdan/sh project on GitHub.
Can I turn off shfmt formatting for part of a file?
No. Formatting cannot be disabled for ranges of lines.
Can shfmt be used from JavaScript or Docker?
A third-party npm package, sh-syntax, bundles the parser and formatter compiled to WASM. Release tags are also published as Docker images.
Linthelm is independent and is not affiliated with mvdan. Product names belong to their owners. Features change; confirm details on the official website.