//home | my other blog | github | twitter | about | legal

Make your scripts better with shellcheck

Being a system engineer (or a fan of a UNIX-like OS) would most certainly mean that one is also writing shell scripts. If this is a daily thing, a pattern has probably emerged in the way the scripts are written, a certain style is used in all those scripts. This does not necessarily mean that the style is good (how to define good?) or that the scripts are completely correct and portable.

Many programming languages have their own style and syntax check tools (the interpreter or compiler itself could serve as the ultimate validator, but some mistakes in the code might not make the script invalid, and only alter it’s behavior and make it work in unintended ways).

Luckily, a similar tool exists for shell scripts as well.

Please use it, it will help you write portable scripts and spot issues that might only happen in odd an inappropriate moments. Just think about adding an extra space in the wrong place of a full path:

# rm -rf / tmp/bla
         ^^^

… or using an undefined variable in a similar statement:

# rm -rf ${mypath}/*
#           ^^- what if this is undefined?

… or using relative paths and launching your script in the wrong directory?

# rm -rf *

BTW, shellcheck is available on OpenBSD as well, as a package:

doas pkg_add shellcheck