How to use shfmt to better format Shell scripts

Contents

Bash Shell

Yes, formatting is necessary and helps greatly to understand complex code. Despite this, for those who write shell scripts many times, double checking the format can become a tedious task. This post will show you how to shortcut the job using shfmt!

What is it shfmt?

Developed by Dustin Krysak, shfmt is a formatter, Shell analyzer and interpreter. The project itself is hosted on GitHub and has a clear README and clearly laid out repository. The tool was developed in Go and is compatible with POSIX shells, Bash y mksh. This makes shfmt a truly universal program rather than being restricted to just Bash.

installing shfmt

Install shfmt on your snap-enabled Linux distribution (like Ubuntu and Mint), run the following command in your terminal:

sudo snap install shfmt

Installing shfmt with snap

Install shfmt on your RedHat-based Linux distribution / Yum (like RHEL, Centos and Fedora), run the following commands in your terminal:

Note: you will have to restart your machine (or log out and log back in) after running the first command and before running the following commands.

sudo dnf install snapd
sudo snap install snap-store
sudo snap install shfmt

In RHEL and Centos, it is feasible that you must also install the The EPEL repository first.

Using shfmt

Once the instant package is installed, you can start using shfmt.

shfmt: a compact tool for formatting Shell

Let's define a written script with very bad format as test.sh as follows:

#!/bin/bash__
 echo 'not well formatted line 1'
  echo 'not well formatted line 2'
echo 'this line has extra spaces on the end > '            
func() {
  echo 'more unneeded spaces'
              echo 'way out'
 }
func ()

there are several problems with this script, the most prominent is the format of the same. But there is also a mistake / error in script: the function call func en la última línea va seguida de más corchetes. A function call in Bash (A function call in Bash) A function call in Bash, A function call in Bash. A function call in Bash.

Let's see what shfmt A function call in Bash.

A function call in Bash

A working example of shfmt failing on wrong function call

While the output seems a bit cryptic, note that the term foo (used here) and bar (not used here now) are often used in IT circles to indicate / represent any language or equivalent element. foo here it really refers to func.

Even then, the message remains a bit cryptic until we realize, looking at the last line, that what is really happening is the beginning of a function definition (and not a function call) because the two brackets were included. This then explains why the message tells us that something else is expected.; must be followed by a statement. shmft you are looking here for something like func(){ some_command[s]; }.

¡Bingo! This increases the functionality of shfmt to be a verification tool / shell script validation, even though it's probably not nearly as complete as the one we wrote about using shellcheck to find and fix scripting errors. Even so, very useful!

We fixed our error and now the input script. test.sh says the following:

#!/bin/bash__
 echo 'not well formatted line 1'
  echo 'not well formatted line 2'
echo 'this line has extra spaces on the end > '            
func() {
  echo 'more unneeded spaces'
              echo 'way out'
 }
func

We run again shfmt against code and you get much more proper and well formatted output:

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

Excellent. Now we can take this level further and tell you shfmt we would like to use an indentation width / two-space tab instead of a full tab. I always write code using two spaces as the indentation width / tabulation, and I use an extra space where a command on the next line is closely related to the previous one, as a continuous command, etc., even though this doesn't happen often. For more than 10 years, I have found the two spaces to be ideal for personal and shared projects.

Each and every project has to find its own ideal syntax, but note that if you use a large tab (8 spaces) as the format presented by shfmt in the example above, your code can become more difficult to read easily.

We will set the indentation width / tab in two spaces using the -i option (that he --help set as Bleeding: 0 for eyelashes (predetermined),> 0 for the number of spaces): shfmt -i 2 test.sh A function call in Bash:

#!/bin/bash__
echo 'not well formatted line 1'
echo 'not well formatted line 2'
echo 'this line has extra spaces on the end > '
func() {
  echo 'more unneeded spaces'
  echo 'way out'
}
func

Excellent! Despite this, we notice that shfmt A function call in Bash: #!/bin/bash__ A function call in Bash #!/bin/bash instead of.

A function call in Bash

So, A function call in Bash shfmt to better format the scripts. Despite this, curiously, in this particular circumstance, inclusive shellcheck did not realize the problem. This deficiency was reported to the shellcheck team, so in due time it can be arranged.

If you are interested in learning more about Linux, you can check out the Bash Automation series & Scripting Basics, as well as the Bash Loops: for, while y until Bash Loops: for, while y until y Exporting Variables in Bash: the Posts why and how.

Ending

Be able to write clean scripts, well formatted and without errors becomes easier work when you use a shell formatting tool like shfmt and a bug checker / mistakes like shellcheck. Even then, as we saw, some things may go unnoticed even up to the moment you run the script for the first time. shfmt is a small but effective utility that will help you to format your scripts and code according to the selected indents. Enjoy!

Subscribe to our Newsletter

We will not send you SPAM mail. We hate it as much as you.