Home > Basic Linux-related things, Linux, Programming > ShellCheck: shell script analysis

ShellCheck: shell script analysis

December 8th, 2012

Shell scripting is notoriously full of pitfalls, unintuitive behavior and poor error messages. Here are some things you might have experienced:

  • find -exec fails on commands that are perfectly valid
  • 0==1 is apparently true
  • Comparisons are always false, and write files while failing
  • Variable values are available inside loops, but reset afterwards
  • Looping over filenames with spaces fails, and quoting doesn’t help

 

ShellCheck is my latest project. It will check shell scripts for all of the above, and also tries to give helpful tips and suggestions for otherwise working ones. You can paste your script and have it checked it online, or you can downloaded it and run it locally.

Other things it checks for includes reading from and redirecting to a file in the same pipeline, useless uses of cat, apparent variable use that won’t expand, too much or too little quoting in [[ ]], not quoting globs passed to find, and instead of just saying “syntax error near unexpected token `fi’”, it points to the relevant if statement and suggests that you might be missing a ‘then’.

It’s still in the early stages, but has now reached the point where it can be useful. The online version has a feedback button (in the top right of your annotated script), so feel free to try it out and submit suggestions!

Basic Linux-related things, Linux, Programming , ,

  1. A name
    December 8th, 2012 at 14:57 | #1

    Could you add checks for bashims and other non-portable junk?

    - warn on use of [[ ]] when [ ] would work
    - #!/bin/bash instead of #!/bin/sh
    etc

  2. Vidar
    December 8th, 2012 at 19:24 | #2

    @A name
    It already warns about some bashisms when using #!/bin/sh, and this could definitely be expanded and improved. Thanks!

    If the user wants to write a bash/zsh/ksh script instead of a sh script, though, I’d be inclined to let them as long as they declare it in the shebang.

  3. Prem
    February 21st, 2013 at 22:34 | #3

    Very Cool & useful tool .. Thanks

  4. mike
    May 23rd, 2013 at 14:44 | #4

    Thanks for this extremely useful tool. Would you consider adding a \Clear\ button that clears the \Paste shell script\ edit box, in order to be able to paste the next shell script?

  5. Vidar
    May 23rd, 2013 at 17:14 | #5

    @mike
    Done!

  6. mike
    May 24th, 2013 at 10:32 | #6

    @Vidar
    Thanks!

  7. Vidar
    June 30th, 2013 at 19:50 | #7

    @A name
    As of today, ShellCheck does the majority of checks that ‘checkbashisms’ would have when #!/bin/sh is used.

  8. Marcelo
    April 28th, 2014 at 19:58 | #8

    Hi Vidar,

    Congratulations for your great work!

    I’m trying to run ShellCheck locally in a Red Hat 6.4 but when I try to install cabal (to build ShellCheck) I got:

    > sudo yum install cabal-install

    –> Finished Dependency Resolution
    Error: Package: ghc-base-devel-4.3.1.0-46.el6.x86_64 (epel)
    Requires: libffi-devel

    And if I try to use a ShellCheck built previously in Ubuntu I got:

    > shellcheck file.sh

    shellcheck: error while loading shared libraries: libgmp.so.10: cannot open shared object file: No such file or directory

    Could you help with this?

  9. Vidar
    May 3rd, 2014 at 02:49 | #9

    @Marcelo
    Did you try installing the additional dependencies yum complains about?

  10. Marcelo
    May 5th, 2014 at 13:03 | #10

    Yes:

    > sudo yum install libffi-devel


    No package libffi-devel available.
    Error: Nothing to do

  11. Vidar
    May 8th, 2014 at 06:30 | #11

    @Marcelo
    Well, I don’t know. If you can build on Ubuntu, you can try
    make clean; ghc --make shellcheck -optl-static -optl-pthread to compile a statically linked version that doesn’t depend on external libraries like libgmp.

  12. Marcelo
    May 8th, 2014 at 14:24 | #12

    Great! It seems that it worked pretty well! If I find any issue I will let you know. Consider to put this in the README.md file.

    Thanks a lot. Congratulations again!

  1. No trackbacks yet.

Ignore the following captcha and enter 'bunnies' in the field instead.