Format Shell Scripts With shfmt

Reformat bash/sh code and show a diff before changing files.

LinuxlintingshellscriptingLow risk
Run shfmt -d script.sh to print the formatting diff without modifying the file.

01Command

Command
$ shfmt -d script.sh

02What 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

RiskLow risk
Requirementssudo apt install shfmt
!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)
    Official project · vendor-doc · 2026-09-20
    Evidence: Debian packages shfmt, a shell script formatter and parser.
  • Debian package: shfmt
    Debian · vendor-doc · 2026-09-20
    Evidence: Debian packages shfmt, a shell script formatter and parser.

Last verified: 2026-09-22 · Category: Developer