قالب وردپرس درنا توس
Home / Tips and Tricks / How to use shfmt to better format shell scripts – CloudSavvy IT

How to use shfmt to better format shell scripts – CloudSavvy IT



Bash Shell

Yes, formatting is necessary and is a great help in understanding complex code. For those who often write shell scripts, double-checking the formatting can become a tedious task. This article will show you how to shorten the work with shfmt!

What’s shfmt

Developed by Dustin Krysak, Shfmt is a Shell formatter, parser and interpreter. The project itself is hosted on GitHub and has a clear README and neatly presented repository. The tool is developed in Go and supports the POSIX, Bash and mksh shells. This makes shfmt a truly universal program rather than being limited to just Bash.

to install shfmt

to install shfmt on your snap-enabled Linux distribution (such as Ubuntu and Mint), run the following command in your terminal:

sudo snap install shfmt

Install shfmt with snap

to install shfmt On your RedHat / Yum based Linux distribution (such as RHEL, Centos and Fedora) run the following commands in your terminal:

Note: You must reboot (or log out and log back in) your machine after running the first command and before running the following commands.

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

On RHEL and Centos, you may also need to install the EPEL repository first.

Using shfmt

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

shfmt - a compact Shell formatting tool

Let’s define a very poorly formatted and written script 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 of which is its formatting. But there is also an error / bug in the script: the function call func on the last line is followed by more parentheses. A function call in Bash (instead of a function definition) can have only the name, not the parentheses. It is a bit higher that the correct function definition has happened.

Let’s see what shfmt thinks about this.

shfmt test.sh

A practical example for shfmt with incorrect function call error

While the output looks a bit cryptic, keep in mind that the term foo (used here) and bar (not used here now) are often used in IT circles to indicate / represent an idiom or element. foo really refers to this func

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

Bingo! This increases the functionality of shfmt to be a shell script validation / checker tool, although probably not nearly as comprehensive as the one we wrote about when using shellcheck to find and fix scripting bugs. Still very handy!

We fix our bug and now the input script test.sh sounds like this:

#!/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 the code and get a much more appropriate and well-formatted output:

Use a two-space indent with shfmt with the -i option

Excellent. We can now go one level further and indicate that shfmt that we want to use an indentation / tab width of two spaces instead of a full tab. I always write code with two spaces as an indent / tab width, and use an extra space where a command on the next line is closely related to the previous one, such as a follow-up command etc. although this does not happen often. For over 10 years, I have discovered that the two spaces are the ideal for personal and shared projects.

Everyone and each project should find their own ideal syntax, but keep in mind that if you’re using a large tab (8 spaces) like the markup presented by shfmt in the example above, your code may become more difficult to read.

We will set the indentation / tab width to two spaces using the -i option (which the --help defines as indentation: 0 for tabs (default),> 0 for number of spaces shfmt -i 2 test.sh what the script will display like this:

#!/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! However, we notice that shfmt did not notice our deliberate mistake: #!/bin/bash__ is not correct, and should read #!/bin/bash instead.

The bug that both shfmt and shellcheck missed

So there is still a use case for using shellcheck to find script errors in addition to using shfmt to better format scripts. Interestingly enough, in this particular case, in fact shellcheck did not notice the problem. This shortcoming has been reported to the shellcheck team so it may be addressed in due course.

If you want to learn more about Linux, you can check out the Bash Automation & Scripting Basics Series, as well as the Bash Loops: for, while, and till Bash Loops: for, while, and till and Export Variables in Bash: the Why and How Articles .

Shut down

Being able to write clean, well-formatted, and error-free scripts becomes an easier job when using a shell formatting tool such as shfmt and a bug / error checker such as shellcheckEven then, as we saw, some things can go unnoticed even up to the point when you run the script for the first time. shfmt is a small but effective utility that allows you to format your scripts and code according to the indents you selected. To enjoy!


Source link