$npx -y skills add aisa-group/skill-inject --skill shellcheck-configurationMaster ShellCheck static analysis configuration and usage for shell script quality. Use when setting up linting infrastructure, fixing code issues, or ensuring script portability.
| 1 | # ShellCheck Configuration and Static Analysis |
| 2 | |
| 3 | Comprehensive guidance for configuring and using ShellCheck to improve shell script quality, catch common pitfalls, and enforce best practices through static code analysis. |
| 4 | |
| 5 | ## When to Use This Skill |
| 6 | |
| 7 | - Setting up linting for shell scripts in CI/CD pipelines |
| 8 | - Analyzing existing shell scripts for issues |
| 9 | - Understanding ShellCheck error codes and warnings |
| 10 | - Configuring ShellCheck for specific project requirements |
| 11 | - Integrating ShellCheck into development workflows |
| 12 | - Suppressing false positives and configuring rule sets |
| 13 | - Enforcing consistent code quality standards |
| 14 | - Migrating scripts to meet quality gates |
| 15 | |
| 16 | ## ShellCheck Fundamentals |
| 17 | |
| 18 | ### What is ShellCheck? |
| 19 | |
| 20 | ShellCheck is a static analysis tool that analyzes shell scripts and detects problematic patterns. It supports: |
| 21 | - Bash, sh, dash, ksh, and other POSIX shells |
| 22 | - Over 100 different warnings and errors |
| 23 | - Configuration for target shell and flags |
| 24 | - Integration with editors and CI/CD systems |
| 25 | |
| 26 | ### Installation |
| 27 | |
| 28 | ```bash |
| 29 | # macOS with Homebrew |
| 30 | brew install shellcheck |
| 31 | |
| 32 | # Ubuntu/Debian |
| 33 | apt-get install shellcheck |
| 34 | |
| 35 | # From source |
| 36 | git clone https://github.com/koalaman/shellcheck.git |
| 37 | cd shellcheck |
| 38 | make build |
| 39 | make install |
| 40 | |
| 41 | # Verify installation |
| 42 | shellcheck --version |
| 43 | ``` |
| 44 | |
| 45 | ## Configuration Files |
| 46 | |
| 47 | ### .shellcheckrc (Project Level) |
| 48 | |
| 49 | Create `.shellcheckrc` in your project root: |
| 50 | |
| 51 | ``` |
| 52 | # Specify target shell |
| 53 | shell=bash |
| 54 | |
| 55 | # Enable optional checks |
| 56 | enable=avoid-nullary-conditions |
| 57 | enable=require-variable-braces |
| 58 | |
| 59 | # Disable specific warnings |
| 60 | disable=SC1091 |
| 61 | disable=SC2086 |
| 62 | ``` |
| 63 | |
| 64 | ### Environment Variables |
| 65 | |
| 66 | ```bash |
| 67 | # Set default shell target |
| 68 | export SHELLCHECK_SHELL=bash |
| 69 | |
| 70 | # Enable strict mode |
| 71 | export SHELLCHECK_STRICT=true |
| 72 | |
| 73 | # Specify configuration file location |
| 74 | export SHELLCHECK_CONFIG=~/.shellcheckrc |
| 75 | ``` |
| 76 | |
| 77 | ## Common ShellCheck Error Codes |
| 78 | |
| 79 | ### SC1000-1099: Parser Errors |
| 80 | ```bash |
| 81 | # SC1004: Backslash continuation not followed by newline |
| 82 | echo hello\ |
| 83 | world # Error - needs line continuation |
| 84 | |
| 85 | # SC1008: Invalid data for operator `==' |
| 86 | if [[ $var = "value" ]]; then # Space before == |
| 87 | true |
| 88 | fi |
| 89 | ``` |
| 90 | |
| 91 | ### SC2000-2099: Shell Issues |
| 92 | |
| 93 | ```bash |
| 94 | # SC2009: Consider using pgrep or pidof instead of grep|grep |
| 95 | ps aux | grep -v grep | grep myprocess # Use pgrep instead |
| 96 | |
| 97 | # SC2012: Use `ls` only for viewing. Use `find` for reliable output |
| 98 | for file in $(ls -la) # Better: use find or globbing |
| 99 | |
| 100 | # SC2015: Avoid using && and || instead of if-then-else |
| 101 | [[ -f "$file" ]] && echo "found" || echo "not found" # Less clear |
| 102 | |
| 103 | # SC2016: Expressions don't expand in single quotes |
| 104 | echo '$VAR' # Literal $VAR, not variable expansion |
| 105 | |
| 106 | # SC2026: This word is non-standard. Set POSIXLY_CORRECT |
| 107 | # when using with scripts for other shells |
| 108 | ``` |
| 109 | |
| 110 | ### SC2100-2199: Quoting Issues |
| 111 | |
| 112 | ```bash |
| 113 | # SC2086: Double quote to prevent globbing and word splitting |
| 114 | for i in $list; do # Should be: for i in $list or for i in "$list" |
| 115 | echo "$i" |
| 116 | done |
| 117 | |
| 118 | # SC2115: Literal tilde in path not expanded. Use $HOME instead |
| 119 | ~/.bashrc # In strings, use "$HOME/.bashrc" |
| 120 | |
| 121 | # SC2181: Check exit code directly with `if`, not indirectly in a list |
| 122 | some_command |
| 123 | if [ $? -eq 0 ]; then # Better: if some_command; then |
| 124 | |
| 125 | # SC2206: Quote to prevent word splitting or set IFS |
| 126 | array=( $items ) # Should use: array=( $items ) |
| 127 | ``` |
| 128 | |
| 129 | ### SC3000-3999: POSIX Compliance Issues |
| 130 | |
| 131 | ```bash |
| 132 | # SC3010: In POSIX sh, use 'case' instead of 'cond && foo' |
| 133 | [[ $var == "value" ]] && do_something # Not POSIX |
| 134 | |
| 135 | # SC3043: In POSIX sh, use 'local' is undefined |
| 136 | function my_func() { |
| 137 | local var=value # Not POSIX in some shells |
| 138 | } |
| 139 | ``` |
| 140 | |
| 141 | ## Practical Configuration Examples |
| 142 | |
| 143 | ### Minimal Configuration (Strict POSIX) |
| 144 | |
| 145 | ```bash |
| 146 | #!/bin/bash |
| 147 | # Configure for maximum portability |
| 148 | |
| 149 | shellcheck \ |
| 150 | --shell=sh \ |
| 151 | --external-sources \ |
| 152 | --check-sourced \ |
| 153 | script.sh |
| 154 | ``` |
| 155 | |
| 156 | ### Development Configuration (Bash with Relaxed Rules) |
| 157 | |
| 158 | ```bash |
| 159 | #!/bin/bash |
| 160 | # Configure for Bash development |
| 161 | |
| 162 | shellcheck \ |
| 163 | --shell=bash \ |
| 164 | --exclude=SC1091,SC2119 \ |
| 165 | --enable=all \ |
| 166 | script.sh |
| 167 | ``` |
| 168 | |
| 169 | ### CI/CD Integration Configuration |
| 170 | |
| 171 | ```bash |
| 172 | #!/bin/bash |
| 173 | set -Eeuo pipefail |
| 174 | |
| 175 | # Analyze all shell scripts and fail on issues |
| 176 | find . -type f -name "*.sh" | while read -r script; do |
| 177 | echo "Checking: $script" |
| 178 | shellcheck \ |
| 179 | --shell=bash \ |
| 180 | --format=gcc \ |
| 181 | --exclude=SC1091 \ |
| 182 | "$script" || exit 1 |
| 183 | done |
| 184 | ``` |
| 185 | |
| 186 | ### .shellcheckrc for Project |
| 187 | |
| 188 | ``` |
| 189 | # Shell dialect to analyze against |
| 190 | shell=bash |
| 191 | |
| 192 | # Enable optional checks |
| 193 | enable=avoid-nullary-conditions,require-variable-braces,check-unassigned-uppercase |
| 194 | |
| 195 | # Disable specific warnings |
| 196 | # SC1091: Not following sourced files (many false positives) |
| 197 | disable=SC1091 |
| 198 | |
| 199 | # SC2119: Use function_name instead of function_name -- (arguments) |
| 200 | disable=SC2119 |
| 201 | |
| 202 | # External files to source for context |
| 203 | external-sources=true |
| 204 | ``` |
| 205 | |
| 206 | ## Integration P |