Skip to main content
  1. 2025/
  2. 📓 Posts from 🗓️ January/

Shellcheck for the Win

Stuff I think is interesting, cool, or otherwise worth sharing™️.

What’s ShellCheck? #

Shellcheck finds bugs in your shell scripts…

it’s pretty neat, imo. According to the shellcheck site; ShellCheck1 is…

  • GPLv32: free as in freedom
  • Documented on the ShellCheck Wiki3
  • Available on GitHub4 (as is the shellcheck website5)
  • Already packaged for your distro or package manager
  • Supported as an integrated linter in major editors
  • Available in CodeClimate, Codacy and CodeFactor to auto-check your GitHub repo
  • Written in Haskell, if you’re into that sort of thing.

What do I do with it? #

According to the shellcheck README; The goals of ShellCheck are to point out situations where:

Typical syntax issues that cause a shell to give cryptic error messages.

Subtle caveats, edge cases and pitfalls that may cause an advanced user’s otherwise working script to fail under future circumstances.

typical intermediate level semantic problems that cause a shell to behave strangely and counter-intuitively.

See the gallery of bad code for examples of what ShellCheck can help you identify!


So… all that being said … #

Here’s me using the diff output format piped to patch to automagically fix some nits.

loiosh@Kiera:~/$ shellcheck wpl_cert.sh  -f diff
--- a/wpl_cert.sh
+++ b/wpl_cert.sh
@@ -5,10 +5,10 @@
 _REHASHBIN=/usr/bin/c_rehash
 _UPDATENEEDED=0
 for CA in ${CA}; do
-  for CERT in `ls ${CACERTDIR}/${CA}`; do
+  for CERT in $(ls ${CACERTDIR}/"${CA}"); do
     _L="${CA}/${CERT}"
     echo -n "${_L}:";
-    if [[ `grep -c ${_L} /etc/ca-certificates.conf` -lt 1 ]]; then
+    if [[ $(grep -c "${_L}" /etc/ca-certificates.conf) -lt 1 ]]; then
       _UPDATENEEDED=1;
       echo " update to /etc/ca-certificates.conf needed"
       echo "${_L}" >> /etc/ca-certificates.conf
loiosh@Kiera:~/$ patch -p1 <<< $(shellcheck wpl_cert.sh  -f diff)
patching file wpl_cert.sh

Closing Thoughts #

Seriously, I’ve been pretty impressed with the tool, and have learned a bunch of neat tricks using it regularly.

Do yerself a favor, and check it out!

Until next time!

❤️🐺W