$linuxjunkies
>

shfmt

A command-line tool that automatically formats shell scripts (bash, sh, ksh) to follow consistent style conventions, similar to how gofmt works for Go.

shfmt is a formatter for shell scripts that reads your code and rewrites it with standardized indentation, spacing, and alignment. It helps teams maintain consistent code style without manual formatting effort.

The tool supports multiple shell dialects including bash, POSIX sh, and ksh. You can run it as shfmt script.sh to print formatted output, or use shfmt -w script.sh to write changes back to the file.

Example: A messy script like if[ $x -eq 1 ];then echo test;fi gets reformatted with proper spacing and indentation: if [ $x -eq 1 ]; then echo test; fi

shfmt is commonly integrated into CI/CD pipelines and editor plugins to enforce style automatically during development.

Related terms