shfmt(1)
shfmt formats shell script files according to a consistent style.
Synopsis
shfmt [OPTION]... [FILE]...Description
shfmt is a shell script formatter that automatically formats bash, posix, and mksh scripts to improve readability and enforce consistent code style. It reads shell files and outputs formatted code, making it useful both as a standalone tool and integrated into editors.
When no files are specified, shfmt reads from standard input. By default, formatted output goes to stdout; use -w to write changes directly to files.
Common options
| Flag | What it does |
|---|---|
-w | write result to file instead of stdout |
-d | show diffs instead of printing formatted output |
-i SPACES | indent size in spaces (default 1) |
-bn | binary operators at start of line in indented blocks |
-ci | indent switch cases |
-sr | space after redirect operators |
-kp | keep padding for alignment |
-ln bash|posix|mksh | language dialect (default bash) |
-s | simplify code (remove unnecessary semicolons and quoting) |
--version | show version and exit |
Examples
format and print a script to stdout
shfmt script.shformat script.sh in-place
shfmt -w script.shshow differences for all shell scripts in current directory
shfmt -d *.shformat with 4-space indentation and write back to file
shfmt -i 4 -w script.shformat and simplify by removing unnecessary quoting and semicolons
shfmt -s script.shformat script from stdin as POSIX shell
cat script.sh | shfmt -ln posixformat all scripts with operators at line start and indented case statements
shfmt -w -bn -ci *.sh