We have all sorts of linter for many languages, but how about shell scripts? Especially, the Bash? We do have some tools for Bash, here is a couple of projects that I know of.

1   bashlint

bashlint is written in Python, to me, it’s more like PEP8, as in checking coding style. It hasn’t really had linting rules. If you are familiar with pylint, which has tons of checking tasks. bashlint is far from that stage. As of writing, version 0.1.0 (2014-06-08), it only checks for three:

W201 Trailing whitespace
Trailing whitespaces are superfluous.
W202 Blank line contains whitespace
Trailing whitespaces on blank lines are superfluous.
W203 Trailing semicolon
Trailing semicolons are superfluous.

Its usage is:

$ bashlint -h
Usage: bashlint [options] input ...

Options:
  -h, --help     show this help message and exit
  -v, --verbose  print debug messages
  --show-source  show source code for each error

If we generate a bad.sh with the following code, it would be having a trailing space in first line, a single tab character in second line, and last line with a trailing semicolon:

echo -e 'echo foobar \n\t\necho ;' > bad.sh

It will give an result like:

$ bashlint .
./bad.sh:1:11: W201 Trailing whitespace
./bad.sh:2:0: W202 Blank line contains whitespace
./bad.sh:3:5: W203 Trailing semicolon

You can also let it point out the error with source code:

$ bashlint --show-source .
./bad.sh:1:11: W201 Trailing whitespace
echo foobar
           ^
./bad.sh:2:0: W202 Blank line contains whitespace

^
./bad.sh:3:5: W203 Trailing semicolon
echo ;
     ^

bashlint is written by Kudriashev Stanislav under the MIT License, currently version 0.1.0 (2014-06-08), it works for both Python 2 and 3.

2   shellcheck

shellcheck is different, more like linter, but doesn’t seem to have style check.

For a source code like:

a=1
b = 2
c=3
echo $a != $b

It returns:

   1  a=1
   2  b = 2
        ^––
SC1068 Don't put spaces around the = in assignments.

   3  c=3
      ^––
SC2034 c appears unused. Verify it or export it.

   4  echo $a != $b

I first discovered shellcheck a year ago at 2013-06-11T16:53:44Z, it seems still be very active, gets commits almost every week.

shellcheck is written in Haskell by Vidar Holen under GNU Affero General Public Licens Version 3, currently version 0.3.3 (2014-05-30), it also has provided an online checker, which is good for people like me doesn’t have Haskell installed, but an API service would be even better. But I think that would be asking too much.