What is `shfmt` and why use it alongside ShellCheck?
BeginnerConceptShellCheck, debugging, testing, portability and security
Answer
shfmt formats shell code consistently (indentation, spacing, case layout), like gofmt for shell. ShellCheck finds bugs; shfmt ends style debates in code review.
shfmt -d -i 2 -ci scripts/ # show diff: 2-space indent, indent case branches
shfmt -w -i 2 -ci scripts/ # rewrite in place
Run it in CI with -d so unformatted code fails the build.
More on ShellCheck, debugging, testing, portability and security
What is ShellCheck and how do you use it in a team?BeginnerHow do you disable a ShellCheck warning, and when is that acceptable?IntermediateName the ShellCheck warnings you see most and what each means.IntermediateHow do you debug a Bash script?BeginnerHow do you make `set -x` output more useful with `PS4`?IntermediateWhat is bats, and how do you test a script that calls external commands?Intermediate