-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Directive
Shellcheck directives allow you to control how shellcheck works, and can be added:
-
As entries in a
.shellcheckrcfile in the project's root-, or user's home-, directory:# Don't suggest [ -n "$VAR" ] over [ ! -z "$VAR" ] disable=SC2236 # Suggest ${VAR} in place of $VAR enable=require-variable-braces
-
and/Or as comments inside individual script files:
hexToAscii() { # shellcheck disable=SC2059 printf "\x$1" }
Directives that replace or are immediately after the shebang apply to the entire script.
Otherwise, they are scoped to the command that follows it. (including compound commands like function definitions, loops and case statements)
-
A directive may only be applied to a complete command, and can not be used immediately preceding an
elseblock or individualcasebranch:# Directive VALID here, applies to whole `case` case $1 in # Directive INVALID, `-v)` is not a complete command -v) # Directive VALID here, applies to whole `if` if [ "$v" ] then # Directive VALID here, applies to `die ..` command die "Only one -v allowed" # Directive INVALID here, `else` is not a complete command else v=1 fi ;; esac
-
There is no support for scoping a directive to the first structure of the script.
In these cases, use a dummy command liketrueor:and then add the directives, such as:# This directive applies to the entire script # shellcheck disable=2086 true # This directive only applies to this function # shellcheck disable=2043 f() { ... }
It is recommended to add a comment to document why a specific directive was used.
-
The comment can be added on the preceding line.
(This is the recommended style for long comments.)# this is intentional because of reasons # that are long and need explaining # shellcheck disable=SC1234 statement_where_warning_should_be_disabled
-
The comment can also be added at the end of the directive line.
(This is the recommended style for short comments.) s# shellcheck disable=SC1234 # this is intentional statement_where_warning_should_be_disabled
Unless the --norc option is used;
-
shellcheckwill look for a file named.shellcheckrcorshellcheckrc(note without the dot),
in the script's directory and each parent directory of that.
If it is not found there, it will look for it in the user'sHOMEdirectory (fe.~/.shellcheckrc),
followed by theXDG configdirectory.
(usually~/.config/shellcheckrcon Unix, or%APPDATA%/shellcheckrcon Windows)Only the first-found file will be used.
-
If found, it will read
key=valuepairs from it and treat them as file-wide directives.
Example.shellcheckrclooks like:# Look for 'source'd files relative to the checked script, # and also look for absolute paths in /mnt/chroot source-path=SCRIPTDIR source-path=/mnt/chroot # Since 0.9.0, values can be quoted with '' or "" to allow spaces source-path="My Documents/scripts" # Allow opening any 'source'd file, even if not specified as input external-sources=true # Turn on warnings for unquoted variables with safe values enable=quote-safe-variables # Turn on warnings for unassigned uppercase variables enable=check-unassigned-uppercase # Allow [ ! -z foo ] instead of suggesting -n disable=SC2236
-
-
ℹ️Note for Snap users:
The Snap sandbox disallows access to hidden files.
Useshellcheckrcwithout the dot instead. -
ℹ️Note for Docker users:
shellcheckwill only be able to look for files that are mounted in the container, so~/.shellcheckrccan not be accessed nor read.
-
4-digits (with or without
SCprefixed)
Preventsshellcheckfrom processing one or more warnings:# shellcheck disable=code[,code...] statement_where_warning_should_be_disabled-
A range can also be specified, handy when disabaling multiple warnings/errors for the entire file.
#!... # shellcheck disable=SC1000-SC9999 # shellcheck disable=1000-9999
ℹ️Silencing parser errors is purely cosmetic; Any parser error found will still stop
shellcheckat the point of the error. -
-
keyphrase
(since 0.7.0)
Enables one or more optional checks.#!... # shellcheck enable=require-variable-braces echo "Hello $USER" # Will suggest to change into ${USER}
-
To see a list of optional checks with examples, run:
shellcheck --list-optional
-
-
boolean (true|false|0|1)
(since v0.8.0)
Allows or disallowsshellcheckto include the files pointed-to by thesourceor.statements while checking.
external-sources=falsedisables this, which is the default.- In
.shellcheckrcsetexternal-sources=trueto enable it globally.
(Individual script files can disable but not enable this option.)
- In
-
single filename (with or without a leading path)
(since v0.4.0)
Dictatesshellcheckwhere to find the to-be-sourced file.# shellcheck source=src/examples/config.sh . "$(locate_config)"
-
see
SCRIPTDIRin source-path (since v0.7.0)?# shellcheck source=SCRIPTDIR/examples/config.sh source "${my_SCRIPTDIR}/config.sh"
#!... # shellcheck source-path=SCRIPTDIR/examples ... source config.sh
-
-
filepath (absolute or relative)
(since 0.7.0)
Informsshellcheckwhere to search for sourced files.# The script will now look for src/examples/mylib.sh # shellcheck source-path=src/examples . mylib.sh
-
The special value
SCRIPTDIRwill search in the current script's directory, and it can be used as a relative path likeSCRIPTDIR/../lib.
To support the common pattern of"${SOME_VAR_NAME}/mylib.sh",shellcheckstrips one leading, dynamic section, before trying to locate the rest.
That means thatshellcheckwill look forconfig.shandutils.shin the same directory as the script itself when it is being checked in the below example:#!... # shellcheck source-path=SCRIPTDIR here="$( cd "$( dirname "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )" . "$here/config.sh" . "$here/utils.sh"
-
-
type of shell (sh|bash||...)
(since v0.4.5)
Specifies the shell dialect for a script.This is similar to a shebang/hashbang/etc, if you for any reason don't want to add one.
# shellcheck shell=sh echo foo &> bar
-
filepath
(since v0.10.0)
Dictatesshellcheckto prefer the specified configuration file, instead of searching for one in the default locations first.See
.shellcheckrcfile for the contents of this file.
-
For a listing of other options not mentioned here, and actually supported by your version of
shellcheck, try one or both of these commands: 😉man shellcheckshellcheck --help