$linuxjunkies
>

shfmt(1)

shfmt formats shell script files according to a consistent style.

UbuntuDebianFedoraArch

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

FlagWhat it does
-wwrite result to file instead of stdout
-dshow diffs instead of printing formatted output
-i SPACESindent size in spaces (default 1)
-bnbinary operators at start of line in indented blocks
-ciindent switch cases
-srspace after redirect operators
-kpkeep padding for alignment
-ln bash|posix|mkshlanguage dialect (default bash)
-ssimplify code (remove unnecessary semicolons and quoting)
--versionshow version and exit

Examples

format and print a script to stdout

shfmt script.sh

format script.sh in-place

shfmt -w script.sh

show differences for all shell scripts in current directory

shfmt -d *.sh

format with 4-space indentation and write back to file

shfmt -i 4 -w script.sh

format and simplify by removing unnecessary quoting and semicolons

shfmt -s script.sh

format script from stdin as POSIX shell

cat script.sh | shfmt -ln posix

format all scripts with operators at line start and indented case statements

shfmt -w -bn -ci *.sh

Related commands