Linting and Improving Your Bash Scripts with ShellCheck
In this video we'll go over how ShellCheck can help us find both subtle and obvious bugs / issues with in shell scripts.
Quick Jump: Outcome of Running ShellCheck against a Script
ShellCheck is one of those things where once you start using it, you can’t imagine life without it. It’s a static analysis tool for shell scripts and it’s comparable to flake8 in Python and Rubocop in Ruby if you’ve ever used those tools before.
The basic idea is it’ll lint your shell scripts and help you discover inconsistencies and potential bugs by showing you warnings. It also helps you stick to using best practices and when it comes to Bash – I’m happy to take any help I can get!
ShellCheck is a standalone command line tool that you can install, and you can even integrate it with your favorite code editor if you want.
Outcome of Running ShellCheck against a Script
Timestamped Table of Contents
- 1:20 – Running ShellCheck against our broken script
- 1:59 – Making sure your script has a shebang defined at the top of the file
- 2:34 – Crawling through ShellCheck warnings one by one
- 3:18 – You can only define local variables in a function with Bash, not POSIX shell
- 4:16 – Help detect invalid commands by splitting variable references and assignments
- 5:48 – If you want to interpolate variables, use double quotes not single quotes
- 7:34 – Debugging an error and using ShellCheck’s output to help fix a typo
- 9:15 – Importance of splitting out your variable’s reference and assignment
- 10:54 – Ignoring a specific ShellCheck warning
- 12:10 – ShellCheck is very nice to hook up with your continuous integration server
- 12:47 – An example of using ShellCheck with Travis-CI for an open source project
- 13:56 – I use ShellCheck all the time in my own scripts and it’s easy to install
- 14:29 – Messing around with https://shellcheck.net without having to install it locally
What was the last thing ShellCheck helped you fix? Let me know below.