Format Shell Scripts With shfmt
Reformat bash/sh code and show a diff before changing files.
Run shfmt -d script.sh to print the formatting diff without modifying the file.
01Command
Command
$ shfmt -d script.sh02What it does
shfmt parses POSIX shell and bash and rewrites indentation, spacing and line continuations deterministically. The -d flag shows a diff, -w writes in place and -i 2 sets indentation, which makes it easy to enforce a house style in CI.
03Risk
!Warning: Formatting rewrites code; review the diff before using -w, since unusual quoting may be reformatted.
04Undo
✓No undo needed — this command does not change persistent state.
05Sources
- mvdan/sh (shfmt)Evidence: Debian packages shfmt, a shell script formatter and parser.
- Debian package: shfmtEvidence: Debian packages shfmt, a shell script formatter and parser.
Last verified: 2026-09-22 · Category: Developer