ShellCheck for Visual Studio Code
Integrates ShellCheck into VS Code, a linter for Shell scripts.
Quick start
Disclaimer
This VS Code extension requires ShellCheck (the awesome static analysis tool for shell scripts) to work, but precompiled ShellCheck binaries are bundled in this extension for these platforms:
- Linux (
x86_64,arm64,arm) - macOS (
x86_64,arm64) - Windows (
x86_64,arm64with thex86_64binary)
Troubleshooting
If ShellCheck seems not working, a helper command ShellCheck: Collect Diagnostics For Current Document from the Command Palette is provided to help troubleshooting.
Options
There are various options that can be configured by making changes to your user or workspace preferences.
Default options are:
shellcheck.ignorePatterns
The shellcheck.ignorePatterns works exactly the same as search.exclude, read more about glob patterns here.
For example:
To add additional ignore patterns atop the default patterns, you have to copy the default ignore patterns and then add yours to the end of the list (#1196).
Fix all errors on save
The auto-fixable errors can be fixed automatically on save by using the following configuration:
Alternatively, you can fix all errors on demand by running the command Fix All in the VS Code Command Palette.
Lint onType or onSave
By default the linter will lint as you type. Alternatively, set shellcheck.run to onSave if you want to lint only when the file is saved (works best if auto-save is on).
Configuring ShellCheck
ShellCheck has a default set of checks, but it is also configurable using RC files. To configure your project, add a .shellcheckrc at the workspace root. If no .shellcheckrc is found in any of the parent directories, ShellCheck will look in ~/.shellcheckrc followed by the $XDG_CONFIG_HOME (usually ~/.config/shellcheckrc) on Unix, or %APPDATA%/shellcheckrc on Windows. Only the first file found will be used.
Here is an example .shellcheckrc:
# 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 ShellCheck 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
As last resort, you can also add the SC identifiers to shellcheck.exclude extension setting. For example, to exclude SC1017:
Using ShellCheck through Docker
In order to get it working, you need a shim script. Avoid including ShellCheck arguments in it.
Here is a simple shim script to get started with (see discussion: #24):
#!/bin/bash exec docker run --rm -i -v "${PWD}:${PWD}:ro" -w "${PWD}" koalaman/shellcheck:latest "$@"
For example, you can place it at shellcheck.sh in the root of your workspace with execution permission (chmod +x shellcheck.sh).
You can can then configure the extension to use it with:
Just have in mind that this should come with a performance hit, as booting up a docker container is slower than just invoking the binary.
Advanced usage
Integrating other VS Code extensions
This extension provides a small API, which allows other VS Code extensions to interact with the ShellCheck extension. For details, see API.md.
Acknowledgements
This extension was originally based on @hoovercj's Haskell Linter.
License
This extension is licensed under the MIT license.
Bundled ShellCheck binaries are licensed under GPLv3.
