LinuxCommandLibrary

shellcheck

Analyze shell scripts for errors

TLDR

Check a shell script

$ shellcheck [path/to/script.sh]
copy

Check a shell script interpreting it as the specified shell dialect (overrides the shebang at the top of the script)
$ shellcheck [[-s|--shell]] [sh|bash|dash|ksh] [path/to/script.sh]
copy

Ignore one or more error types
$ shellcheck [[-e|--exclude]] [SC1009,SC1073,...] [path/to/script.sh]
copy

Also check any sourced shell scripts
$ shellcheck [[-a|--check-sourced]] [path/to/script.sh]
copy

Display output in the specified format (defaults to tty)
$ shellcheck [[-f|--format]] [tty|checkstyle|diff|gcc|json|json1|quiet] [path/to/script.sh]
copy

Enable one or more [o]ptional checks
$ shellcheck [[-o|--enable]] [add-default-case,avoid-nullary-conditions,...] [path/to/script.sh]
copy

List all available optional checks that are disabled by default
$ shellcheck --list-optional
copy

Adjust the level of severity to consider (defaults to style)
$ shellcheck [[-S|--severity]] [error|warning|info|style] [path/to/script.sh]
copy

SYNOPSIS

shellcheck [options] scriptfile...

PARAMETERS

-a
    Alias 'source' to '.'. Useful with dash.

-A
    Shows all style issues, including ones shellcheck normally hides.

-C
    Sets the checkstyle output format. Useful for integration with tools expecting this format.

-d level
    Debug level (0-3). 0: disabled, 1: shows tokens/AST, 2: adds type information, 3: adds more internal info. Useful for debugging shellcheck itself.

-e code1,code2,...
    Excludes the specified error codes.

-f format
    Sets the output format: 'text', 'checkstyle', 'json', 'diff'.

-H
    Lists available error codes.

-i code1,code2,...
    Includes only the specified error codes.

-l
    Lists files with style issues; omits actual style issues.

-o option1,option2,...
    Sets shell dialect options (e.g., bash=no_unset). See documentation for details.

-p shell
    Specifies the shell dialect (e.g., sh, bash, dash, ksh, zsh).

-q
    Quiet mode. Only prints errors, not style issues or usage information.

-s shell
    Synonym for -p.

-S severity
    Selects error severity (error, warning, style, info).

-t
    Use a temporary directory (TMPDIR) for shell scripts.

-V
    Prints the version number.

-x
    Treats script files as xargs input; each line is a file.

DESCRIPTION

Shellcheck is a static analysis tool for shell scripts. It primarily detects and warns about common syntax errors, semantic problems, and subtle caveats that can lead to unexpected behavior. It helps to improve the reliability and maintainability of shell scripts by identifying issues before they cause problems during execution. It can be integrated into development workflows, editors, and CI/CD pipelines for automated code quality checks. Shellcheck supports various shell dialects like bash, sh, dash, ksh, and zsh, and its ruleset is constantly updated to reflect best practices and emerging patterns. It provides detailed error messages and suggestions for fixes, making it a valuable asset for both novice and experienced shell script writers. Think of it as a linter for shell scripts, offering a proactive approach to preventing bugs and ensuring code clarity.

CAVEATS

Shellcheck is a static analyzer, so it cannot detect all possible runtime errors. It's a good starting point but doesn't replace thorough testing.

EXIT CODES

Shellcheck exits with 0 if no errors are found, 1 if any errors are found, and 2 if an unrecoverable error occurred during processing.

HISTORY

Shellcheck was created by Vidar Holen. It has gained significant traction in recent years due to the increasing importance of DevOps and the need for robust and reliable shell scripting practices. The tool is actively maintained with regular updates and new features. Its adoption has been driven by the widespread use of shell scripts in automation and infrastructure management.

SEE ALSO

sh(1), bash(1), dash(1)

Copied to clipboard