Skip to content

Bash

gspot checks shell scripts for mistakes with ShellCheck, for layout with shfmt, and for Zsh syntax with zsh -n:

Check Stage Level What it does
bash/zsh-syntax commit basic Checks the syntax of Zsh scripts with zsh -n.
bash/shellcheck commit basic Runs ShellCheck over shell scripts, with its stable optional rules at level all.
bash/shfmt commit basic Checks that every shell script matches the shfmt output, with the layout from .editorconfig.
bash/script-layout commit all Checks each Bash script for a shebang, strict mode, a final main call when it defines functions, declarative libraries, and removed temporary files.
bash/unused-functions commit all Finds shell functions no script in the scope calls.
bash/unread-arguments commit basic Finds shell functions called with arguments they never read.
bash/function-size commit all Checks every shell function against the size ceilings: code lines, branches, nesting, and assignments.
bash/inline-scripts commit all Finds inline Python and Node code, and heredocs piped into an interpreter, in shell scripts.
bash/safety commit basic Finds broad process kills, recursive deletes outside traps that remove mktemp paths, and unsafe shell forms such as typeset, eval, and curl without --fail.

Each tool reads the scripts of its own shell:

Scripts Tools
Bash, sh, dash, and bats ShellCheck and shfmt
Zsh zsh -n, through bash/zsh-syntax
Every shell script, ksh included The built-in checks, such as bash/safety

ShellCheck reads a script without a shebang as Bash, and it reads each file that a script loads with source. Of the built-in checks, bash/script-layout reads only Bash scripts. gspot install does not install zsh, so install it yourself when your repository has Zsh scripts.

At level basic, ShellCheck runs its default checks, and bash/safety reports commands that kill processes by name, such as killall, and recursive deletes. It also reports eval, curl without --fail, typeset, |&, bash -lc, and GNU timeout.

At level all, gspot turns on optional ShellCheck checks, such as quote-safe-variables and require-double-brackets.

A script that pipes a download into a shell gets two findings at both levels:

scripts/deploy.sh
curl https://example.com/install.sh | bash
failed bash/safety 2 findings
scripts/deploy.sh:1 curl-fail Pass `--fail`, so that an HTTP error stops the script.
scripts/deploy.sh:1 curl-pipe-shell Nobody can read a script that runs as it downloads. Download it to a file, check it, then run it.

shfmt reads its layout options from .editorconfig, which gspot writes from the format settings. When most lines of your .sh and .bash files use another indent, gspot init writes a [[format.override]] record for them, and you change the indent in that record. shfmt also indents the branches of a case statement and simplifies code, such as removing $ inside $(( )).

At level all, bash/function-size allows each shell function 40 lines, 8 branches, 3 levels of nesting, and 8 assignments. The limits.bash settings change these limits, and raising one needs a reason:

gspot set limits.bash.max_function_lines 60 --reason "The deploy script keeps its steps in one function."

To turn off one ShellCheck code, ignore it with a reason, or allow one line with a comment, such as # shellcheck disable=SC2086 # reason: the word splitting is wanted here:

gspot ignore bash/shellcheck --rule SC2086 --reason "The build flags split into words on purpose."

ShellCheck skips four codes in .bats files that do not fit how bats runs tests. At level all, the naming checks accept the variables that the shell defines, such as IFS, and ignore a leading underscore.