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.

  1. No trackbacks yet.

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