Shell formatter CLI for reformatting and syntax-checking Bash, POSIX, mksh, bats, and zsh scripts.
$go install mvdan.cc/sh/v3/cmd/shfmt@latest
Agent Compatibility
JSON Output
Agent Skill
MCP Support
AI Analysis
shfmt is a shell formatter for normalizing script layout across repos, CI, and editor workflows. It also walks directories, detects shell files by extension or shebang, and can parse scripts into a typed shell AST.
What It Enables
- Reformat shell scripts consistently across a repo, write fixes in place, or show diffs and changed-file lists for CI and pre-commit checks.
- Find shell scripts recursively and validate syntax across Bash, POSIX sh, mksh, bats, and zsh without depending on a particular runtime shell.
- Convert shell source to a typed JSON syntax tree, transform or inspect it, and print it back to shell code with
--from-json.
Agent Fit
- The core commands are non-interactive, deterministic, and easy to compose in edit-then-verify loops for shell code changes.
- Repo-wide runs, stdin support, explicit exit failures on diffs or parse errors, and EditorConfig handling make it practical in scripts, CI, and automated refactors.
- Structured output exists, but mainly as an AST via
--to-json; normal formatting and directory-walk flows still return plain text paths, diffs, and errors.
Caveats
- Formatting is intentionally opinionated, with limited style knobs and no support for disabling formatting on selected line ranges.
--to-jsononly works with stdin, so machine-readable output is not the default interface for batch formatting runs.