bash
Shell scripts, hooks, and task files: lint, formatting, syntax, and the structure rules for shell. Guide: Bash.
Selected when
Section titled “Selected when”A scope has file extensions .sh, .bash, .zsh, or .bats, or a file with a shebang for shell.
Checks
Section titled “Checks”zsh-syntax
Section titled “zsh-syntax”Checks the syntax of Zsh scripts with zsh -n.
Runs at the commit stage.
Zsh syntax differs from Bash, so a Bash parser rejects valid Zsh programs.
What to do
Section titled “What to do”Fix the syntax at the line Zsh reports. Install zsh when the system lacks it.
Example
Section titled “Example”A Zsh script whose if block has no closing fi reports a parse error at the end of the file. Close the block with fi.
Turn it off
Section titled “Turn it off”gspot ignore bash/zsh-syntax --paths "<path>" --reason "<why>"shellcheck
Section titled “shellcheck”Runs ShellCheck over shell scripts, with its stable optional rules at level all.
Runs at the commit stage.
Quoting, globbing, and exit-status mistakes pass on normal input, and fail on the first file name with a space.
What to do
Section titled “What to do”Read the SC code in the finding; gspot explain bash/shellcheck SC2086 explains one code.
Example
Section titled “Example”In a Bash script, printf "%s\n" $name reports SC2086 because the expansion permits word splitting and globbing. Use printf "%s\n" "$name" to pass the value as one argument.
Turn it off
Section titled “Turn it off”gspot ignore bash/shellcheck --paths "<path>" --reason "<why>"gspot ignore bash/shellcheck --rule <rule> --reason "<why>"To silence one place, write a comment such as:
# shellcheck disable=SC2086 # reason: the word splitting is wanted hereChecks that every shell script matches the shfmt output, with the layout from .editorconfig.
Runs at the commit stage. gspot check --fix fixes what it can.
One formatter means diffs show what changed, not how someone’s editor indents.
What to do
Section titled “What to do”Run the fix: command that the report prints.
Example
Section titled “Example”if true;then echo hello;fi differs from the shfmt output. Run gspot check --fix to add the spacing and line breaks.
Turn it off
Section titled “Turn it off”gspot ignore bash/shfmt --paths "<path>" --reason "<why>"script-layout
Section titled “script-layout”Checks each Bash script for a shebang, strict mode, a final main call when it defines functions, declarative libraries, and removed temporary files.
Runs at the commit stage, only at the all level.
A script without strict mode runs on after a failed command, and one without a shebang runs in whichever shell starts it.
What to do
Section titled “What to do”Open with the shebang and run set -euo pipefail first. When an executable defines functions, end it with a call to main.
Example
Section titled “Example”An executable Bash script without set -euo pipefail reports a finding. Put that line before the first command, and when the script defines functions, call main last.
Lint rules
Section titled “Lint rules”| Rule | What it checks | What to do |
|---|---|---|
shebang |
A Bash script starts with the shebang #!/usr/bin/env bash or #!/bin/bash. |
Write #!/usr/bin/env bash on the first line. |
strict-mode |
An executable script runs set -euo pipefail before its first command. |
Add set -euo pipefail as the first command after the shebang. |
main-function |
A script with functions has one entry point, a function named main. |
Move the top-level commands into one main function. |
main-call |
The last line of a script with functions calls main "$@". |
End the script with main "$@". |
executable-bit |
A script that ends with main "$@" has the executable bit. |
Run git update-index --chmod=+x <path>. |
library-flow |
A sourced library only declares functions and variables at the top level. | Move the command into a function, and let the script that sources the library call it. |
library-options |
Only the executable script sets shell options, never a library that it sources. | Remove the set command, and let the executable script set its options. |
library-main |
Only an executable script defines and calls main. |
Rename the function after what it does, and remove the call. |
library-exit |
No top-level exit runs in a sourced library. |
Return a status from a function instead. |
mktemp-trap |
A trap removes each path that mktemp creates. |
Add trap 'rm -rf -- "$<name>"' EXIT after the mktemp line. |
Turn it off
Section titled “Turn it off”gspot ignore bash/script-layout --paths "<path>" --reason "<why>"gspot ignore bash/script-layout --rule shebang --reason "<why>"unused-functions
Section titled “unused-functions”Finds shell functions no script in the scope calls.
Runs at the commit stage, only at the all level, once per scope.
A function that nothing calls still costs reading and upkeep, and it hides which code runs.
What to do
Section titled “What to do”Delete the function.
Example
Section titled “Example”A private function _orphan that no script calls reports a finding. Delete it, or record a hook that needs it with gspot ignore and a reason.
Turn it off
Section titled “Turn it off”gspot ignore bash/unused-functions --paths "<path>" --reason "<why>"unread-arguments
Section titled “unread-arguments”Finds shell functions called with arguments they never read.
Runs at the commit stage, once per scope.
An unread argument misleads the caller, who expects it to change the result.
What to do
Section titled “What to do”Read the argument in the function, or drop it from every call.
Example
Section titled “Example”_deaf one two reports dead parameters when _deaf never reads its arguments. Drop them from the call, or use them in the function.
Turn it off
Section titled “Turn it off”gspot ignore bash/unread-arguments --paths "<path>" --reason "<why>"function-size
Section titled “function-size”Checks every shell function against the size ceilings: code lines, branches, nesting, and assignments.
Runs at the commit stage, only at the all level.
A long script holds more than one job, and many branches or assignments hide several functions under one name.
What to do
Section titled “What to do”Split the script or the function, or raise the named limits.bash setting with a reason.
Example
Section titled “Example”A function over limits.bash.max_function_lines reports function-lines with the count and the limit. Split it by job.
Lint rules
Section titled “Lint rules”| Rule | What it checks | What to do |
|---|---|---|
function-lines |
A function has at most limits.bash.max_function_lines code lines. |
Split the function by the steps it performs. |
function-branches |
A function has at most limits.bash.max_function_branches branches. |
Move the branches of one decision into a function of their own. |
block-nesting |
A function nests blocks at most limits.bash.max_block_nesting levels deep. |
Return early, or move the inner block into a function of its own. |
function-assignments |
A function assigns at most limits.bash.max_function_assignments variables. |
Split the function by the steps it performs, and pass values between them. |
Turn it off
Section titled “Turn it off”gspot ignore bash/function-size --paths "<path>" --reason "<why>"gspot ignore bash/function-size --rule function-lines --reason "<why>"inline-scripts
Section titled “inline-scripts”Finds inline Python and Node code, and heredocs piped into an interpreter, in shell scripts.
Runs at the commit stage, only at the all level.
No check reads code inside a heredoc.
What to do
Section titled “What to do”Move the embedded code into its own file and run that.
Example
Section titled “Example”A shell heredoc containing a Python program reports embedded code. Move the program into a .py file and invoke that file from the shell script.
Turn it off
Section titled “Turn it off”gspot ignore bash/inline-scripts --paths "<path>" --reason "<why>"safety
Section titled “safety”Finds broad process kills, recursive deletes outside traps that remove mktemp paths, and unsafe shell forms such as typeset, eval, and curl without --fail.
Runs at the commit stage.
A broad kill or a recursive delete can destroy more than the script meant to.
What to do
Section titled “What to do”Stop one process by its ID, delete only paths the script created, handle each failure, and write the form that the message names.
Example
Section titled “Example”killall worker reports broad-kill. Stop the one process by its ID, or ignore the rule for that script with a reason.
Lint rules
Section titled “Lint rules”| Rule | What it checks | What to do |
|---|---|---|
broad-kill |
No command stops processes by name with pkill or killall. |
Keep the ID of the process that the script starts, and stop that process with kill. |
recursive-remove |
No rm -r deletes a path other than a mktemp path in a trap. |
Create the folder with mktemp -d, and remove it in a trap. When the script must delete another path, ignore the rule. |
ignored-failure |
At level all, no command discards its failure by falling back to true. |
Check the status and act on it, or let set -e stop the script. |
echo |
At level all, messages print with printf, not echo. |
Write printf '%s\n' "<message>". |
login-shell |
No command runs bash -c as a login shell. |
Leave out -l and --login. |
gnu-timeout |
No command calls timeout or gtimeout. |
Pass the time limit to the tool itself, such as curl --max-time 30. |
curl-fail |
Each curl call passes --fail. |
Add --fail or -f to the call. |
trace-mode |
At level all, committed code does not turn on set -x. |
Remove set -x, and turn tracing on only when you debug. |
eval |
No command calls eval. |
Build the command as an array of words, and expand the array in quotes to run it. |
function-keyword |
At level all, no function definition uses the function keyword. |
Write name() { ...; }, without the function keyword. |
typeset |
Variables use local, declare, or readonly, not typeset. |
Use local, declare, or readonly instead of typeset. |
combined-redirect |
At level all, redirects write >file 2>&1, not &>. |
Write >file 2>&1 instead of &>. |
combined-pipe |
Pipes that also carry standard error write 2>&1 before the pipe. |
Redirect standard error with 2>&1 before the pipe. |
brace-for |
A for loop uses do and done, not a brace block. |
Write for arg in "$@"; do ... done. |
seq-loop |
At level all, a counting loop uses for (( ... )), not seq. |
Count with for (( index = 0; index < count; index++ )), not seq. |
script-ifs |
At level all, the script sets IFS only in a function or a subshell. |
Set IFS with local IFS in a function or in a subshell, and do not save it to restore it. |
curl-pipe-shell |
No download runs in a shell as it arrives. | Save the download to a file, verify its checksum, and run the file. |
Turn it off
Section titled “Turn it off”gspot ignore bash/safety --paths "<path>" --reason "<why>"gspot ignore bash/safety --rule broad-kill --reason "<why>"Rules for coding agents
Section titled “Rules for coding agents”| Rule file | Installed when |
|---|---|
.gspot/rules/BASH.md |
Always |
.gspot/rules/OPERATIONS.md |
Always |
.gspot/rules/SAFETY.md |
Always |
Tools and versions
Section titled “Tools and versions”| Tool | Version |
|---|---|
| shellcheck | 0.11.0 |
| shfmt | 3.14.1 |
| zsh | Any, install it yourself |
Files gspot writes
Section titled “Files gspot writes”.gspot/configurations/bash/shellcheckrc