talira

Shell script formatter, with a Go shell parser and interpreter library (mvdan/sh)

open sourceBSD-3-Clausefree

shfmt formats POSIX Shell, Bash, Zsh and mksh scripts in one consistent style, and converts scripts to and from a JSON syntax tree. It comes from mvdan/sh, a pure-Go shell library whose packages parse scripts into a syntax tree, split, quote and expand words like a shell, and interpret scripts without a system shell, including on Windows, with handlers that control what a script can run and access.

Ways to install

Problems it solves

  • format my shell scripts consistently in CI
  • check bash scripts are formatted in a pre-commit hook
  • run shell scripts from a Go program without a system shell

Features

  • Formats POSIX Shell, Bash, Zsh and mksh scripts
  • Converts between shell and a JSON syntax tree
  • Go parser that builds a shell syntax tree
  • Interpreter that runs scripts without a system shell, including on Windows
  • Shell-style word splitting, quoting, expansion and globbing in Go
  • Docker images with Alpine variants
  • Editor integrations for VS Code, JetBrains IDEs, Vim, Emacs and Prettier

Limitations

  • The formatter cannot be turned off for ranges of lines3
  • Does not support the $(( and (( ambiguity; space the operands3
  • Subshells in the interpreter run as goroutines, so real PIDs and file descriptors cannot be used3

Alternatives

The same job

These do what shfmt does.

  1. pretty-quick Runs Prettier on only the files you changed, with a pre-commit mode

Compare shfmt with the top 3