[H1][BASH-SCRIPT-VALIDATOR]
Dictum: Layered validation catches defects that single-pass analysis misses.
Validate bash scripts via syntax check, ShellCheck 0.11.0+ static analysis, and data-driven custom checks.
Tasks:
- Run
bash scripts/validate.sh <script-path> — Syntax, static analysis, custom checks.
- Review errors/warnings/info from output.
- Reference docs/ for fix patterns:
- Suggest fixes — Before/after with line numbers.
[1][VALIDATION_LAYERS]
Dictum: Each layer targets a distinct defect class.
| [INDEX] |
[LAYER] |
[CHECK] |
[TOOL] |
| [1] |
Syntax |
bash -n / sh -n |
Built-in |
| [2] |
Static analysis |
SC codes, 4 severity levels (SC2327-SC2335 in 0.11) |
ShellCheck 0.11.0+ |
| [3] |
Security |
eval injection, unsafe rm, pipe-to-shell, dyn source |
_SECURITY_CHECKS dispatch table |
| [4] |
Performance |
UUOC, $(cat) -> $(<), date subshell |
_PERF_CHECKS dispatch table |
| [5] |
Portability |
Bashisms in sh scripts ([[, arrays, source, ==) |
_SH_BASHISM_CHECKS dispatch table |
| [6] |
Best practice |
printf > echo, [[ ]] > [ ], mapfile > while-read |
_PRACTICE_CHECKS dispatch table |
Guidance:
- Data-Driven: Custom checks use
declare -Ar tables with "pattern|message|level" format and generic nameref runner.
- Architecture:
_run_check_set() iterates via local -n _checks=$1, splitting each entry with IFS='|' read -r.
[2][EXIT_CODES]
Dictum: Exit codes enable pipeline composition.
| [INDEX] |
[CODE] |
[MEANING] |
| [1] |
0 |
Clean |
| [2] |
1 |
Warnings only |
| [3] |
2 |
Errors found |
[3][FILE_MAP]
Dictum: File inventory enables targeted loading.
| [INDEX] |
[FILE] |
[PURPOSE] |
| [1] |
scripts/validate.sh |
Main validator (shebang detection, assoc array counters). |
| [2] |
scripts/ensure_shellcheck.sh |
Asserts shellcheck presence (Nix-provided; apt/dnf fallback). |
| [3] |
docs/shell-reference.md |
Bash 5.2+/5.3 vs POSIX sh, FP patterns, data structures. |
| [4] |
docs/shellcheck-reference.md |
SC codes, v0.11.0 additions, directives, CI. |
| [5] |
docs/text-tools.md |
rg/sd/awk/regex, bash-native alternatives. |
| [6] |
examples/good.sh |
Best practices (bash + POSIX). |
| [7] |
examples/bad.sh |
Anti-patterns (bash + POSIX). |
[REFERENCE]: →shell-reference.md — Language features, FP patterns.
[REFERENCE]: →shellcheck-reference.md — ShellCheck codes, directives.
[REFERENCE]: →text-tools.md — Modern text tools (rg/sd/awk), bash-native alternatives.
1---2name: bash-script-validator3description: Validates existing .sh/.bash scripts via ShellCheck 0.11.0+ static analysis, syntax/security/portability checks. Use when debugging SC codes, auditing shell scripts, or checking shell best practices.4---5
6# [H1][BASH-SCRIPT-VALIDATOR]
7>**Dictum:** *Layered validation catches defects that single-pass analysis misses.*
8
9<br>
10
11Validate bash scripts via syntax check, ShellCheck 0.11.0+ static analysis, and data-driven custom checks.
12
13**Tasks:**
141. Run `bash scripts/validate.sh <script-path>` — Syntax, static analysis, custom checks.
152. Review errors/warnings/info from output.
163. Reference docs/ for fix patterns:
17 - [→shell-reference.md](./docs/shell-reference.md) — Bash 5.2+/5.3 vs POSIX sh, parameter expansion, FP patterns.
18 - [→shellcheck-reference.md](./docs/shellcheck-reference.md) — SC codes, v0.11.0 additions, directives, CI integration.
19 - [→text-tools.md](./docs/text-tools.md) — rg/sd/awk/regex, bash-native alternatives.
204. Suggest fixes — Before/after with line numbers.
21
22---
23## [1][VALIDATION_LAYERS]
24>**Dictum:** *Each layer targets a distinct defect class.*
25
26<br>
27
28| [INDEX] | [LAYER] | [CHECK] | [TOOL] |
29| :-----: | ------------------- | ----------------------------------------------------- | ----------------------------------- |
30| [1] | **Syntax** | `bash -n` / `sh -n` | Built-in |
31| [2] | **Static analysis** | SC codes, 4 severity levels (SC2327-SC2335 in 0.11) | ShellCheck 0.11.0+ |
32| [3] | **Security** | eval injection, unsafe rm, pipe-to-shell, dyn source | `_SECURITY_CHECKS` dispatch table |
33| [4] | **Performance** | UUOC, `$(cat)` -> `$(<)`, date subshell | `_PERF_CHECKS` dispatch table |
34| [5] | **Portability** | Bashisms in sh scripts (`[[`, arrays, `source`, `==`) | `_SH_BASHISM_CHECKS` dispatch table |
35| [6] | **Best practice** | printf > echo, `[[ ]]` > `[ ]`, mapfile > while-read | `_PRACTICE_CHECKS` dispatch table |
36
37**Guidance:**<br>
38- *Data-Driven:* Custom checks use `declare -Ar` tables with `"pattern|message|level"` format and generic nameref runner.
39- *Architecture:* `_run_check_set()` iterates via `local -n _checks=$1`, splitting each entry with `IFS='|' read -r`.
40
41---
42## [2][EXIT_CODES]
43>**Dictum:** *Exit codes enable pipeline composition.*
44
45<br>
46
47| [INDEX] | [CODE] | [MEANING] |
48| :-----: | :----: | ------------- |
49| [1] | **0** | Clean |
50| [2] | **1** | Warnings only |
51| [3] | **2** | Errors found |
52
53---
54## [3][FILE_MAP]
55>**Dictum:** *File inventory enables targeted loading.*
56
57<br>
58
59| [INDEX] | [FILE] | [PURPOSE] |
60| :-----: | ---------------------------------- | ------------------------------------------------------------- |
61| [1] | **`scripts/validate.sh`** | Main validator (shebang detection, assoc array counters). |
62| [2] | **`scripts/ensure_shellcheck.sh`** | Asserts shellcheck presence (Nix-provided; apt/dnf fallback). |
63| [3] | **`docs/shell-reference.md`** | Bash 5.2+/5.3 vs POSIX sh, FP patterns, data structures. |
64| [4] | **`docs/shellcheck-reference.md`** | SC codes, v0.11.0 additions, directives, CI. |
65| [5] | **`docs/text-tools.md`** | rg/sd/awk/regex, bash-native alternatives. |
66| [6] | **`examples/good.sh`** | Best practices (bash + POSIX). |
67| [7] | **`examples/bad.sh`** | Anti-patterns (bash + POSIX). |
68
69[REFERENCE]: [→shell-reference.md](./docs/shell-reference.md) — Language features, FP patterns.<br>
70[REFERENCE]: [→shellcheck-reference.md](./docs/shellcheck-reference.md) — ShellCheck codes, directives.<br>
71[REFERENCE]: [→text-tools.md](./docs/text-tools.md) — Modern text tools (rg/sd/awk), bash-native alternatives.