diff --git a/Directive.md b/Directive.md index b5dc297..1c17aa2 100644 --- a/Directive.md +++ b/Directive.md @@ -1,59 +1,69 @@ Shellcheck directives allow you to control how `shellcheck` works, and take the form of comments in files: - hexToAscii() { - # shellcheck disable=SC2059 - printf "\x$1" - } +```sh +hexToAscii() { + # shellcheck disable=SC2059 + printf "\x$1" +} +``` Supported directives are ### disable Prevent shellcheck from processing one or more warnings: - # shellcheck disable=code[,code...] - statement_where_warning_should_be_disabled +```sh +# shellcheck disable=code[,code...] +statement_where_warning_should_be_disabled +``` ### source Tell ShellCheck where to find a sourced file (since 0.4.0): - # shellcheck source=src/examples/config.sh - . "$(locate_config)" +```sh +# shellcheck source=src/examples/config.sh +. "$(locate_config)" +``` ### shell Specify the shell for a script (similar to the shebang, if you for any reason don't want to add one) (since [0.4.5](https://github.com/koalaman/shellcheck/issues/581#issuecomment-249437837)): - # shellcheck shell=sh - echo foo &> bar +```sh +# shellcheck shell=sh +echo foo &> bar +``` 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 `else` block or individual `case` branch: - - # 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 - +```sh +# 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 `true` or `:` and then add directives, such as - # This directive applies to the entire script - # shellcheck disable=2086 - true +```sh +# This directive applies to the entire script +# shellcheck disable=2086 +true - # This directive only applies to this function - # shellcheck disable=2043 - f() { - ... - } +# This directive only applies to this function +# shellcheck disable=2043 +f() { + ... +} +``` Silencing parser errors is purely cosmetic; any parser error found will still stop ShellCheck at the point of the error. \ No newline at end of file