Yes, formatting is necessary and helps tremendously with understanding complex code.

However, for those who write shell scripts often, double checking formatting can become a tedious task.

This article will show you how to shortcut the work using shfmt!

Tux mascot jumping from Windows to Linux.

What Isshfmt?

Developed by Dustin Krysak, shfmt is a Shell formatter, parser and interpreter.

The project itself ishosted on GitHuband has a clear README and cleanly presented repository.

Terminal window showing the manual page for the Mutt email client on Linux

The tool was developed in Go and supports the POSIX, Bash and mksh shells.

This makes shfmt a truly universal program instead of being restricted to Bash only.

Using shfmt

One the snap package is installed, you’re free to start using shfmt.

Article image

It is a bit higher that the proper function definition happened.

Let’s see whatshfmtthinks of this.

Still, very handy indeed!

Installing shfmt with snap

though this does not happen often.

There is thus still a use case for usingshellcheckto find script bugs besides usingshfmtto format scripts better.

However, interestingly, in this particular case, evenshellcheckfailed to notice the issue.

shfmt - a compact tool for Shell formatting

This shortcoming was reported to the shellcheck team so in due time this may be fixed.

A practical example for shfmt with error on incorrect function call

Using a two space indent with shfmt using the -i option

The error both shfmt and shellcheck missed