bash32-lint 0.1.0__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (77) hide show
  1. bash32_lint-0.1.0/.github/workflows/ci.yml +78 -0
  2. bash32_lint-0.1.0/.github/workflows/release.yml +49 -0
  3. bash32_lint-0.1.0/.gitignore +6 -0
  4. bash32_lint-0.1.0/.pre-commit-hooks.yaml +15 -0
  5. bash32_lint-0.1.0/LICENSE +21 -0
  6. bash32_lint-0.1.0/PKG-INFO +239 -0
  7. bash32_lint-0.1.0/README.md +221 -0
  8. bash32_lint-0.1.0/action.yml +46 -0
  9. bash32_lint-0.1.0/pyproject.toml +74 -0
  10. bash32_lint-0.1.0/src/bash32_lint/__init__.py +10 -0
  11. bash32_lint-0.1.0/src/bash32_lint/__main__.py +7 -0
  12. bash32_lint-0.1.0/src/bash32_lint/checker.py +273 -0
  13. bash32_lint-0.1.0/src/bash32_lint/cli.py +155 -0
  14. bash32_lint-0.1.0/src/bash32_lint/lexer.py +581 -0
  15. bash32_lint-0.1.0/src/bash32_lint/py.typed +0 -0
  16. bash32_lint-0.1.0/src/bash32_lint/rules.py +155 -0
  17. bash32_lint-0.1.0/src/bash32_lint/runner.py +100 -0
  18. bash32_lint-0.1.0/tests/conftest.py +5 -0
  19. bash32_lint-0.1.0/tests/fixtures/append-both/bad-append.sh +6 -0
  20. bash32_lint-0.1.0/tests/fixtures/append-both/good.sh +6 -0
  21. bash32_lint-0.1.0/tests/fixtures/assoc-array/bad-declare.sh +7 -0
  22. bash32_lint-0.1.0/tests/fixtures/assoc-array/good.sh +11 -0
  23. bash32_lint-0.1.0/tests/fixtures/case-fallthrough/bad-fallthrough.sh +9 -0
  24. bash32_lint-0.1.0/tests/fixtures/case-fallthrough/bad-test-next.sh +9 -0
  25. bash32_lint-0.1.0/tests/fixtures/case-fallthrough/good.sh +8 -0
  26. bash32_lint-0.1.0/tests/fixtures/case-modification/bad-element-silent.sh +8 -0
  27. bash32_lint-0.1.0/tests/fixtures/case-modification/bad-heredoc.sh +9 -0
  28. bash32_lint-0.1.0/tests/fixtures/case-modification/bad-lower.sh +7 -0
  29. bash32_lint-0.1.0/tests/fixtures/case-modification/good.sh +11 -0
  30. bash32_lint-0.1.0/tests/fixtures/coproc/bad-coproc.sh +7 -0
  31. bash32_lint-0.1.0/tests/fixtures/coproc/good.sh +5 -0
  32. bash32_lint-0.1.0/tests/fixtures/empty-array-nounset/bad-heredoc.sh +9 -0
  33. bash32_lint-0.1.0/tests/fixtures/empty-array-nounset/bad-quoted.sh +7 -0
  34. bash32_lint-0.1.0/tests/fixtures/empty-array-nounset/bad-star-in-loop.sh +7 -0
  35. bash32_lint-0.1.0/tests/fixtures/empty-array-nounset/good-without-nounset.sh +4 -0
  36. bash32_lint-0.1.0/tests/fixtures/empty-array-nounset/good.sh +13 -0
  37. bash32_lint-0.1.0/tests/fixtures/globstar/bad-shopt.sh +6 -0
  38. bash32_lint-0.1.0/tests/fixtures/globstar/good.sh +6 -0
  39. bash32_lint-0.1.0/tests/fixtures/lastpipe/bad-shopt.sh +6 -0
  40. bash32_lint-0.1.0/tests/fixtures/lastpipe/good.sh +5 -0
  41. bash32_lint-0.1.0/tests/fixtures/mapfile/bad-mapfile.sh +6 -0
  42. bash32_lint-0.1.0/tests/fixtures/mapfile/bad-readarray.sh +6 -0
  43. bash32_lint-0.1.0/tests/fixtures/mapfile/good.sh +8 -0
  44. bash32_lint-0.1.0/tests/fixtures/nameref/bad-declare-n.sh +7 -0
  45. bash32_lint-0.1.0/tests/fixtures/nameref/good.sh +8 -0
  46. bash32_lint-0.1.0/tests/fixtures/negative-index/bad-assign.sh +7 -0
  47. bash32_lint-0.1.0/tests/fixtures/negative-index/bad-read.sh +7 -0
  48. bash32_lint-0.1.0/tests/fixtures/negative-index/good.sh +10 -0
  49. bash32_lint-0.1.0/tests/fixtures/negative-length/bad-element.sh +7 -0
  50. bash32_lint-0.1.0/tests/fixtures/negative-length/bad-string.sh +7 -0
  51. bash32_lint-0.1.0/tests/fixtures/negative-length/good.sh +13 -0
  52. bash32_lint-0.1.0/tests/fixtures/new-variables/bad-epochrealtime.sh +6 -0
  53. bash32_lint-0.1.0/tests/fixtures/new-variables/bad-epochseconds.sh +6 -0
  54. bash32_lint-0.1.0/tests/fixtures/new-variables/bad-srandom-arithmetic.sh +6 -0
  55. bash32_lint-0.1.0/tests/fixtures/new-variables/good.sh +9 -0
  56. bash32_lint-0.1.0/tests/fixtures/param-transform/bad-element-silent.sh +8 -0
  57. bash32_lint-0.1.0/tests/fixtures/param-transform/bad-keys.sh +8 -0
  58. bash32_lint-0.1.0/tests/fixtures/param-transform/bad-quote.sh +7 -0
  59. bash32_lint-0.1.0/tests/fixtures/param-transform/bad-upper.sh +7 -0
  60. bash32_lint-0.1.0/tests/fixtures/param-transform/good.sh +8 -0
  61. bash32_lint-0.1.0/tests/fixtures/pipe-stderr/bad-pipe.sh +6 -0
  62. bash32_lint-0.1.0/tests/fixtures/pipe-stderr/good.sh +5 -0
  63. bash32_lint-0.1.0/tests/fixtures/printf-v-array/bad-element.sh +6 -0
  64. bash32_lint-0.1.0/tests/fixtures/printf-v-array/good.sh +7 -0
  65. bash32_lint-0.1.0/tests/fixtures/read-i/bad-read.sh +6 -0
  66. bash32_lint-0.1.0/tests/fixtures/read-i/good.sh +7 -0
  67. bash32_lint-0.1.0/tests/fixtures/test-v/bad-dbracket.sh +7 -0
  68. bash32_lint-0.1.0/tests/fixtures/test-v/bad-test.sh +7 -0
  69. bash32_lint-0.1.0/tests/fixtures/test-v/good.sh +9 -0
  70. bash32_lint-0.1.0/tests/fixtures/wait-n/bad-wait.sh +7 -0
  71. bash32_lint-0.1.0/tests/fixtures/wait-n/good.sh +7 -0
  72. bash32_lint-0.1.0/tests/prove_fixtures.sh +30 -0
  73. bash32_lint-0.1.0/tests/test_checker.py +187 -0
  74. bash32_lint-0.1.0/tests/test_cli.py +87 -0
  75. bash32_lint-0.1.0/tests/test_fixtures.py +61 -0
  76. bash32_lint-0.1.0/tests/test_runner.py +109 -0
  77. bash32_lint-0.1.0/uv.lock +317 -0
@@ -0,0 +1,78 @@
1
+ name: CI
2
+
3
+ on:
4
+ push:
5
+ branches: [main]
6
+ pull_request:
7
+
8
+ permissions:
9
+ contents: read
10
+
11
+ jobs:
12
+ check:
13
+ runs-on: ubuntu-latest
14
+ strategy:
15
+ matrix:
16
+ python: ["3.9", "3.10", "3.11", "3.12", "3.13", "3.14"]
17
+ steps:
18
+ - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
19
+ with:
20
+ persist-credentials: false
21
+ - uses: astral-sh/setup-uv@36300bab3f61b84e19f32b6bd26b7897f3c7e951 # v10.4.2
22
+ with:
23
+ python-version: ${{ matrix.python }}
24
+ - run: uv sync --locked
25
+ - run: uv run ruff check
26
+ - run: uv run ruff format --check
27
+ - run: uv run pyright
28
+ - run: uv run pytest
29
+
30
+ macos:
31
+ # macOS runners ship /bin/bash 3.2, so the fixture proof and the run tests use the real system bash here.
32
+ runs-on: macos-latest
33
+ steps:
34
+ - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
35
+ with:
36
+ persist-credentials: false
37
+ - uses: astral-sh/setup-uv@36300bab3f61b84e19f32b6bd26b7897f3c7e951 # v10.4.2
38
+ - run: /bin/bash --version
39
+ - run: uv sync --locked
40
+ - run: uv run pytest
41
+
42
+ fixtures:
43
+ name: fixtures under bash ${{ matrix.bash }}
44
+ runs-on: ubuntu-latest
45
+ strategy:
46
+ fail-fast: false
47
+ matrix:
48
+ bash: ["3.2", "4.0", "4.1", "4.2", "4.3", "4.4", "5.0", "5.1", "5.2", "5.3"]
49
+ steps:
50
+ - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
51
+ with:
52
+ persist-credentials: false
53
+ - name: Each bad fixture fails before its min-bash version and passes from it on
54
+ env:
55
+ IMAGE: bash:${{ matrix.bash }}
56
+ run: docker run --rm -v "$PWD:/work" -w /work "$IMAGE" bash tests/prove_fixtures.sh tests/fixtures
57
+
58
+ action:
59
+ runs-on: ubuntu-latest
60
+ steps:
61
+ - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
62
+ with:
63
+ persist-credentials: false
64
+ - name: The action passes on a clean script and runs it under bash 3.2
65
+ uses: ./
66
+ with:
67
+ paths: tests/prove_fixtures.sh tests/fixtures/mapfile/good.sh
68
+ run: tests/prove_fixtures.sh
69
+ - name: The action fails on a bad script
70
+ id: bad
71
+ continue-on-error: true
72
+ uses: ./
73
+ with:
74
+ paths: tests/fixtures/mapfile/bad-mapfile.sh
75
+ - name: Check the bad script failed
76
+ env:
77
+ OUTCOME: ${{ steps.bad.outcome }}
78
+ run: test "$OUTCOME" = failure
@@ -0,0 +1,49 @@
1
+ name: Release
2
+
3
+ on:
4
+ release:
5
+ types: [published]
6
+
7
+ permissions:
8
+ contents: read
9
+
10
+ jobs:
11
+ build:
12
+ runs-on: ubuntu-latest
13
+ steps:
14
+ - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
15
+ with:
16
+ persist-credentials: false
17
+ - uses: astral-sh/setup-uv@36300bab3f61b84e19f32b6bd26b7897f3c7e951 # v10.4.2
18
+ with:
19
+ python-version: "3.13"
20
+ - name: The tag names the package's version
21
+ env:
22
+ TAG: ${{ github.event.release.tag_name }}
23
+ run: test "$TAG" = "v$(uv version --short)"
24
+ - run: uv sync --locked
25
+ - run: uv run ruff check
26
+ - run: uv run ruff format --check
27
+ - run: uv run pyright
28
+ - run: uv run pytest
29
+ - run: uv build
30
+ - uses: actions/upload-artifact@cf430e030ddbb5b0abf93d22962f4752f3646cd9 # v7.0.2
31
+ with:
32
+ name: dist
33
+ path: dist/
34
+
35
+ publish:
36
+ needs: build
37
+ runs-on: ubuntu-latest
38
+ environment: pypi
39
+ permissions:
40
+ # Only this job, which runs no project code, gets the OIDC token PyPI's trusted publishing checks.
41
+ id-token: write
42
+ steps:
43
+ - uses: actions/download-artifact@9000827ccba6bdab643e8b6fd33ac0654aef8333 # v8.0.2
44
+ with:
45
+ name: dist
46
+ path: dist/
47
+ - uses: pypa/gh-action-pypi-publish@7245906fb61fa33a13554f9d5fb856fe61758086 # v1.14.3
48
+ with:
49
+ attestations: true
@@ -0,0 +1,6 @@
1
+ .venv/
2
+ __pycache__/
3
+ *.pyc
4
+ .pytest_cache/
5
+ .ruff_cache/
6
+ dist/
@@ -0,0 +1,15 @@
1
+ - id: bash32-lint
2
+ name: bash32-lint
3
+ description: Find bash constructs that break on macOS's /bin/bash 3.2.
4
+ entry: bash32-lint check
5
+ language: python
6
+ types: [shell]
7
+ exclude_types: [zsh]
8
+
9
+ - id: bash32-syntax
10
+ name: bash32-lint syntax under bash 3.2
11
+ description: Parse each script with a real bash 3.2 (local /bin/bash on macOS, else the bash:3.2 image).
12
+ entry: bash32-lint run --syntax-only
13
+ language: python
14
+ types: [shell]
15
+ exclude_types: [zsh]
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Sara Tavares
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
@@ -0,0 +1,239 @@
1
+ Metadata-Version: 2.5
2
+ Name: bash32-lint
3
+ Version: 0.1.0
4
+ Summary: Find bash constructs that break on macOS's /bin/bash 3.2, and run scripts under a real bash 3.2.
5
+ Project-URL: Repository, https://github.com/stavares843/bash32-lint
6
+ Project-URL: Issues, https://github.com/stavares843/bash32-lint/issues
7
+ License-Expression: MIT
8
+ License-File: LICENSE
9
+ Keywords: bash,lint,macos,portability,shell
10
+ Classifier: Development Status :: 3 - Alpha
11
+ Classifier: Environment :: Console
12
+ Classifier: Programming Language :: Python :: 3
13
+ Classifier: Programming Language :: Unix Shell
14
+ Classifier: Topic :: Software Development :: Quality Assurance
15
+ Classifier: Typing :: Typed
16
+ Requires-Python: >=3.9
17
+ Description-Content-Type: text/markdown
18
+
19
+ # bash32-lint
20
+
21
+ Find the bash constructs that break on macOS's `/bin/bash` 3.2, before a Mac
22
+ user runs your script. A static check with a real tokenizer, and an optional
23
+ run under a real bash 3.2.
24
+
25
+ ![Terminal animation of bash32-lint running](https://raw.githubusercontent.com/stavares843/bash32-lint/main/docs/bash32-lint.gif)
26
+
27
+ ```
28
+ $ bash32-lint check deploy.sh
29
+ deploy.sh:4:9: assoc-array declare -A: associative arrays do not exist before bash 4.0 (macOS /bin/bash is 3.2)
30
+ fix: two indexed arrays of keys and values, or a case statement
31
+ deploy.sh:5:6: case-modification ${1,,}: ${var,,} and ${var^^} are a bad substitution, and on an array element are silently ignored, before bash 4.0 (macOS /bin/bash is 3.2)
32
+ fix: $(printf '%s' "$var" | tr '[:upper:]' '[:lower:]')
33
+ deploy.sh:6:11: empty-array-nounset ${extra_flags[@]}: expanding an empty array is an unbound variable error under set -u before bash 4.4 (macOS /bin/bash is 3.2)
34
+ fix: ${extra_flags[@]+"${extra_flags[@]}"}
35
+ deploy.sh:6:41: pipe-stderr |& is a syntax error before bash 4.0 (macOS /bin/bash is 3.2)
36
+ fix: cmd 2>&1 | other
37
+ 4 finding(s)
38
+ ```
39
+
40
+ ## Why
41
+
42
+ macOS still ships bash 3.2.57 as `/bin/bash`, the last release under GPLv2.
43
+ Every `#!/bin/bash` script on a Mac runs under it, and so does
44
+ `#!/usr/bin/env bash` unless the user has installed a newer bash and put it
45
+ first on `PATH`. Linux CI runs bash 5, so a script that passes CI can fail on
46
+ the first Mac that runs it.
47
+
48
+ Some of the failures are loud: `|&`, `&>>`, `;&` and `[[ -v x ]]` are syntax
49
+ errors. Bash runs every command before them, then exits with status 2 when it
50
+ reaches that line (or the function, loop or `if` that holds it), so the script
51
+ stops halfway. Some fail only their own command, and without `set -e` the
52
+ script carries on: `declare -A`, `mapfile`, `${var,,}`. The worst ones are
53
+ quiet or conditional:
54
+
55
+ - `"${args[@]}"` of an empty array under `set -u` aborts with
56
+ `args[@]: unbound variable` on every bash before 4.4, but only on the run
57
+ where the array happens to be empty.
58
+ - `${arr[0],,}` and `${arr[@]@Q}` are not errors on 3.2: the modifier is
59
+ silently dropped and the value comes through unchanged.
60
+ - `$EPOCHSECONDS` and `$SRANDOM` are just unset, so they expand to nothing.
61
+ - `shopt -s globstar` fails, the script carries on, and `**` matches like `*`.
62
+
63
+ ## Install
64
+
65
+ ```sh
66
+ pipx install bash32-lint # or: pip install bash32-lint
67
+ uvx bash32-lint check . # run without installing
68
+ ```
69
+
70
+ Python 3.9 or later, no dependencies. The `run` command also needs a bash 3.2:
71
+ `/bin/bash` on a Mac, or Docker for the `bash:3.2` image elsewhere.
72
+
73
+ ## Usage
74
+
75
+ ```sh
76
+ bash32-lint check scripts/ install.sh # exit 1 on findings, 0 when clean, 2 on a usage error
77
+ bash32-lint check --ignore test-v,wait-n .
78
+ bash32-lint check --format github . # GitHub annotations instead of text
79
+ bash32-lint rules # the rules and their versions
80
+ ```
81
+
82
+ `check` takes files and directories. A file named on the command line is
83
+ always checked, unless its shebang names another shell (zsh, ksh, fish, dash
84
+ and so on). In a directory it checks `*.sh`, `*.bash` and files whose shebang
85
+ runs `bash` or `sh`, and skips `.git`, `node_modules` and virtualenvs.
86
+
87
+ ### Suppressing a finding
88
+
89
+ A `# bash32-ignore <rule>[,<rule>...]` comment silences those rules on its own
90
+ line, or, when the comment is alone on its line, on the next one:
91
+
92
+ ```bash
93
+ declare -A cache # bash32-ignore assoc-array
94
+ # bash32-ignore mapfile
95
+ mapfile -t lines < "$file" # guarded by a version check above
96
+ ```
97
+
98
+ A function you define yourself with the name of a missing builtin, such as a
99
+ `mapfile` shim, turns that command's rule off for the file.
100
+
101
+ ### Running under a real bash 3.2
102
+
103
+ ```sh
104
+ bash32-lint run tests/run.sh --verbose # run a script (and its arguments) under bash 3.2
105
+ bash32-lint run --syntax-only *.sh # only parse each file with bash -n
106
+ bash32-lint run --docker tests/run.sh # use the container even on a Mac
107
+ ```
108
+
109
+ It uses the first bash 3.2 it finds: `--bash PATH`, then `$BASH32`, then
110
+ `/bin/bash`, then `bash` on `PATH`. Without one it runs
111
+ `docker run --rm -v "$PWD:$PWD" -w "$PWD" bash:3.2 bash ...`, so the script must
112
+ be under the current directory (`--image` picks another image). The exit
113
+ status is the script's. Docker is never needed for `check`.
114
+
115
+ ## GitHub Action
116
+
117
+ ```yaml
118
+ - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
119
+ - uses: stavares843/bash32-lint@<commit-sha> # v0.1.0
120
+ with:
121
+ paths: scripts install.sh # space-separated, default "."
122
+ ignore: "" # comma-separated rule ids
123
+ run: tests/run.sh # optional: also run this under bash 3.2
124
+ ```
125
+
126
+ Findings appear as annotations on the pull request. `run` uses `/bin/bash` on
127
+ macOS runners and the `bash:3.2` image on Linux runners. The action needs
128
+ `python3` on the runner, which GitHub's Linux and macOS images have.
129
+
130
+ ## pre-commit
131
+
132
+ ```yaml
133
+ repos:
134
+ - repo: https://github.com/stavares843/bash32-lint
135
+ rev: v0.1.0
136
+ hooks:
137
+ - id: bash32-lint
138
+ - id: bash32-syntax # optional: bash -n under a real bash 3.2 (a Mac, or Docker)
139
+ ```
140
+
141
+ ## Rules
142
+
143
+ The version is the first bash release where the construct works. Each one was
144
+ checked against bash's [NEWS](https://git.savannah.gnu.org/cgit/bash.git/plain/NEWS)
145
+ and [CHANGES](https://git.savannah.gnu.org/cgit/bash.git/plain/CHANGES) files,
146
+ and CI proves it: every bad fixture runs under the official `bash:3.2` to
147
+ `bash:5.3` images and must fail on each version before its own and pass from it
148
+ on.
149
+
150
+ | Rule | Finds | On bash 3.2 | Works from | Source |
151
+ | --- | --- | --- | --- | --- |
152
+ | `empty-array-nounset` | `"${arr[@]}"`, `${arr[*]}` with `set -u` in the file | `arr[@]: unbound variable` when empty | 4.4 | CHANGES, bash-4.4-rc2, 3.a |
153
+ | `assoc-array` | `declare -A`, `local -A`, `typeset -A`, `readonly -A` | invalid option | 4.0 | NEWS, bash-4.0 |
154
+ | `mapfile` | `mapfile`, `readarray` | command not found | 4.0 | NEWS, bash-4.0 |
155
+ | `case-modification` | `${v,,}` `${v,}` `${v^^}` `${v^}` | bad substitution; ignored on `${a[i],,}` | 4.0 | NEWS, bash-4.0 |
156
+ | `pipe-stderr` | `cmd \|& other` | syntax error | 4.0 | NEWS, bash-4.0 |
157
+ | `append-both` | `cmd &>> file` | syntax error | 4.0 | NEWS, bash-4.0 |
158
+ | `coproc` | `coproc` | command not found | 4.0 | NEWS, bash-4.0 |
159
+ | `nameref` | `declare -n`, `local -n`, `typeset -n` | invalid option | 4.3 | NEWS, bash-4.3 |
160
+ | `param-transform` | `${v@Q}` `@E` `@P` `@A` `@a` / `@U` `@u` `@L` `@K` / `@k` | bad substitution; ignored on `${a[@]@Q}` | 4.4 / 5.1 / 5.2 | NEWS, bash-4.4, bash-5.1, bash-5.2 |
161
+ | `printf-v-array` | `printf -v 'arr[i]'` | not a valid identifier | 4.1 | NEWS, bash-4.1 |
162
+ | `negative-index` | `${arr[-1]}` / `arr[-1]=x` | bad array subscript | 4.2 / 4.3 | NEWS, bash-4.2, bash-4.3 |
163
+ | `negative-length` | `${v:0:-1}`, `${a[0]:1: -2}` | `substring expression < 0` | 4.2 | NEWS, bash-4.2 |
164
+ | `globstar` | `shopt -s globstar` | invalid option name, `**` acts as `*` | 4.0 | NEWS, bash-4.0 |
165
+ | `lastpipe` | `shopt -s lastpipe` | invalid option name | 4.2 | NEWS, bash-4.2 |
166
+ | `wait-n` | `wait -n` | invalid option | 4.3 | NEWS, bash-4.3 |
167
+ | `new-variables` | `$EPOCHSECONDS`, `$EPOCHREALTIME` / `$SRANDOM`, also in `$(( ))` | unset: empty, or unbound under `set -u` | 5.0 / 5.1 | NEWS, bash-5.0, bash-5.1 |
168
+ | `read-i` | `read -i default` | invalid option | 4.0 | NEWS, bash-4.0 |
169
+ | `case-fallthrough` | `;&` and `;;&` | syntax error | 4.0 | NEWS, bash-4.0 |
170
+ | `test-v` | `[[ -v x ]]`, `[ -v x ]`, `test -v x` | syntax error in `[[`, unary operator expected in `[` | 4.2 | NEWS, bash-4.2 |
171
+
172
+ What is not reported, because bash 3.2 handles it: `"$@"` and `"$*"` with no
173
+ arguments, `${#arr[@]}` of an empty array, `${arr[@]:-}`, `${arr[@]+...}`,
174
+ `${!arr[@]}`, `${arr[@]: -1}`, `&>`, `shopt -q globstar`, and any of the above
175
+ inside single quotes, comments, quoted heredocs (`<<'EOF'`) or escaped with a
176
+ backslash. An unquoted heredoc body is expanded by bash, so it is checked. An
177
+ expansion with a default, such as `${EPOCHSECONDS:-$(date +%s)}`, is not
178
+ reported.
179
+
180
+ ## Why Python
181
+
182
+ The check has to tell code from text: `${v,,}` inside single quotes, a comment,
183
+ a quoted heredoc or `$'...'` is fine, and the same characters in an unquoted
184
+ heredoc or inside `$( ... )` nested in double quotes are not. That takes a
185
+ tokenizer that tracks quoting, heredocs, `case` patterns (whose `)` is not the
186
+ end of a `$( )`), `[[ ]]`, arithmetic and command substitution. A grep, awk or
187
+ bash scanner gets these wrong; a real tokenizer is easier to write and test in
188
+ a general-purpose language.
189
+
190
+ Between Go and Python, Python has the least install friction for shell users:
191
+ `pipx install` or `uvx` with no compiler, the `python3` that the Xcode Command
192
+ Line Tools put on every developer Mac (3.9) is enough, pre-commit installs
193
+ `language: python` hooks on its own, and the Action installs into a venv with
194
+ the runner's `python3`. Go would need a Go toolchain for `go install` and for
195
+ pre-commit's `golang` hooks, or per-platform release binaries. The package has
196
+ no runtime dependencies.
197
+
198
+ ## Limits
199
+
200
+ - It tokenizes; it does not build a full syntax tree or follow control flow. It
201
+ cannot know whether an array is empty, so every unguarded `${arr[@]}` in a
202
+ file that enables `set -u` anywhere is reported. Guard it, or suppress the
203
+ line.
204
+ - `set -u` is detected per file: from a `set -u`, `set -o nounset` or a
205
+ `#!/bin/bash -u` shebang. A library sourced by a script that sets it is not
206
+ known to run under it.
207
+ - It does not follow `source`, or look inside strings passed to `eval`,
208
+ `bash -c` or `sh -c`. Backtick substitutions are scanned approximately.
209
+ - Options and command names built at run time (`declare "$flags" x`,
210
+ `"$cmd" -n`) are not seen. Aliases are not expanded.
211
+ - The rule list covers the common breakages, not every bash 4+ feature. Not
212
+ checked yet: `{fd}>` named file descriptors (4.1), `declare -l`/`-u` (4.0),
213
+ `declare -g` (4.2), `read -N` (4.1), `$BASHPID` (4.0), `\u` escapes in
214
+ `$'...'` (4.2), and a `**` glob without the `shopt` line in the same file.
215
+ - `run --docker` mounts only the current directory, and the container has the
216
+ `bash:3.2` image's busybox tools, not macOS's BSD ones. It proves the bash, not
217
+ the rest of the Mac.
218
+
219
+ ## Development
220
+
221
+ ```sh
222
+ uv sync
223
+ uv run ruff check && uv run ruff format --check
224
+ uv run pyright
225
+ uv run pytest
226
+ bash tests/prove_fixtures.sh # every fixture under the bash running it
227
+ docker run --rm -v "$PWD:/work" -w /work bash:4.3 bash tests/prove_fixtures.sh
228
+ ```
229
+
230
+ `tests/fixtures/<rule>/` holds `bad-*.sh` files, each with a `# min-bash:`
231
+ version and the `# expect:` line:column of its findings, and a `good.sh` with
232
+ look-alikes that must not be reported: the same text in quotes, comments and
233
+ quoted heredocs, and the bash 3.2 way to do the same thing. `prove_fixtures.sh`
234
+ runs them all under the bash that runs it: on a Mac, the tests run it with
235
+ `/bin/bash` 3.2, and CI runs it in the `bash:3.2` to `bash:5.3` images.
236
+
237
+ ## License
238
+
239
+ MIT
@@ -0,0 +1,221 @@
1
+ # bash32-lint
2
+
3
+ Find the bash constructs that break on macOS's `/bin/bash` 3.2, before a Mac
4
+ user runs your script. A static check with a real tokenizer, and an optional
5
+ run under a real bash 3.2.
6
+
7
+ ![Terminal animation of bash32-lint running](https://raw.githubusercontent.com/stavares843/bash32-lint/main/docs/bash32-lint.gif)
8
+
9
+ ```
10
+ $ bash32-lint check deploy.sh
11
+ deploy.sh:4:9: assoc-array declare -A: associative arrays do not exist before bash 4.0 (macOS /bin/bash is 3.2)
12
+ fix: two indexed arrays of keys and values, or a case statement
13
+ deploy.sh:5:6: case-modification ${1,,}: ${var,,} and ${var^^} are a bad substitution, and on an array element are silently ignored, before bash 4.0 (macOS /bin/bash is 3.2)
14
+ fix: $(printf '%s' "$var" | tr '[:upper:]' '[:lower:]')
15
+ deploy.sh:6:11: empty-array-nounset ${extra_flags[@]}: expanding an empty array is an unbound variable error under set -u before bash 4.4 (macOS /bin/bash is 3.2)
16
+ fix: ${extra_flags[@]+"${extra_flags[@]}"}
17
+ deploy.sh:6:41: pipe-stderr |& is a syntax error before bash 4.0 (macOS /bin/bash is 3.2)
18
+ fix: cmd 2>&1 | other
19
+ 4 finding(s)
20
+ ```
21
+
22
+ ## Why
23
+
24
+ macOS still ships bash 3.2.57 as `/bin/bash`, the last release under GPLv2.
25
+ Every `#!/bin/bash` script on a Mac runs under it, and so does
26
+ `#!/usr/bin/env bash` unless the user has installed a newer bash and put it
27
+ first on `PATH`. Linux CI runs bash 5, so a script that passes CI can fail on
28
+ the first Mac that runs it.
29
+
30
+ Some of the failures are loud: `|&`, `&>>`, `;&` and `[[ -v x ]]` are syntax
31
+ errors. Bash runs every command before them, then exits with status 2 when it
32
+ reaches that line (or the function, loop or `if` that holds it), so the script
33
+ stops halfway. Some fail only their own command, and without `set -e` the
34
+ script carries on: `declare -A`, `mapfile`, `${var,,}`. The worst ones are
35
+ quiet or conditional:
36
+
37
+ - `"${args[@]}"` of an empty array under `set -u` aborts with
38
+ `args[@]: unbound variable` on every bash before 4.4, but only on the run
39
+ where the array happens to be empty.
40
+ - `${arr[0],,}` and `${arr[@]@Q}` are not errors on 3.2: the modifier is
41
+ silently dropped and the value comes through unchanged.
42
+ - `$EPOCHSECONDS` and `$SRANDOM` are just unset, so they expand to nothing.
43
+ - `shopt -s globstar` fails, the script carries on, and `**` matches like `*`.
44
+
45
+ ## Install
46
+
47
+ ```sh
48
+ pipx install bash32-lint # or: pip install bash32-lint
49
+ uvx bash32-lint check . # run without installing
50
+ ```
51
+
52
+ Python 3.9 or later, no dependencies. The `run` command also needs a bash 3.2:
53
+ `/bin/bash` on a Mac, or Docker for the `bash:3.2` image elsewhere.
54
+
55
+ ## Usage
56
+
57
+ ```sh
58
+ bash32-lint check scripts/ install.sh # exit 1 on findings, 0 when clean, 2 on a usage error
59
+ bash32-lint check --ignore test-v,wait-n .
60
+ bash32-lint check --format github . # GitHub annotations instead of text
61
+ bash32-lint rules # the rules and their versions
62
+ ```
63
+
64
+ `check` takes files and directories. A file named on the command line is
65
+ always checked, unless its shebang names another shell (zsh, ksh, fish, dash
66
+ and so on). In a directory it checks `*.sh`, `*.bash` and files whose shebang
67
+ runs `bash` or `sh`, and skips `.git`, `node_modules` and virtualenvs.
68
+
69
+ ### Suppressing a finding
70
+
71
+ A `# bash32-ignore <rule>[,<rule>...]` comment silences those rules on its own
72
+ line, or, when the comment is alone on its line, on the next one:
73
+
74
+ ```bash
75
+ declare -A cache # bash32-ignore assoc-array
76
+ # bash32-ignore mapfile
77
+ mapfile -t lines < "$file" # guarded by a version check above
78
+ ```
79
+
80
+ A function you define yourself with the name of a missing builtin, such as a
81
+ `mapfile` shim, turns that command's rule off for the file.
82
+
83
+ ### Running under a real bash 3.2
84
+
85
+ ```sh
86
+ bash32-lint run tests/run.sh --verbose # run a script (and its arguments) under bash 3.2
87
+ bash32-lint run --syntax-only *.sh # only parse each file with bash -n
88
+ bash32-lint run --docker tests/run.sh # use the container even on a Mac
89
+ ```
90
+
91
+ It uses the first bash 3.2 it finds: `--bash PATH`, then `$BASH32`, then
92
+ `/bin/bash`, then `bash` on `PATH`. Without one it runs
93
+ `docker run --rm -v "$PWD:$PWD" -w "$PWD" bash:3.2 bash ...`, so the script must
94
+ be under the current directory (`--image` picks another image). The exit
95
+ status is the script's. Docker is never needed for `check`.
96
+
97
+ ## GitHub Action
98
+
99
+ ```yaml
100
+ - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
101
+ - uses: stavares843/bash32-lint@<commit-sha> # v0.1.0
102
+ with:
103
+ paths: scripts install.sh # space-separated, default "."
104
+ ignore: "" # comma-separated rule ids
105
+ run: tests/run.sh # optional: also run this under bash 3.2
106
+ ```
107
+
108
+ Findings appear as annotations on the pull request. `run` uses `/bin/bash` on
109
+ macOS runners and the `bash:3.2` image on Linux runners. The action needs
110
+ `python3` on the runner, which GitHub's Linux and macOS images have.
111
+
112
+ ## pre-commit
113
+
114
+ ```yaml
115
+ repos:
116
+ - repo: https://github.com/stavares843/bash32-lint
117
+ rev: v0.1.0
118
+ hooks:
119
+ - id: bash32-lint
120
+ - id: bash32-syntax # optional: bash -n under a real bash 3.2 (a Mac, or Docker)
121
+ ```
122
+
123
+ ## Rules
124
+
125
+ The version is the first bash release where the construct works. Each one was
126
+ checked against bash's [NEWS](https://git.savannah.gnu.org/cgit/bash.git/plain/NEWS)
127
+ and [CHANGES](https://git.savannah.gnu.org/cgit/bash.git/plain/CHANGES) files,
128
+ and CI proves it: every bad fixture runs under the official `bash:3.2` to
129
+ `bash:5.3` images and must fail on each version before its own and pass from it
130
+ on.
131
+
132
+ | Rule | Finds | On bash 3.2 | Works from | Source |
133
+ | --- | --- | --- | --- | --- |
134
+ | `empty-array-nounset` | `"${arr[@]}"`, `${arr[*]}` with `set -u` in the file | `arr[@]: unbound variable` when empty | 4.4 | CHANGES, bash-4.4-rc2, 3.a |
135
+ | `assoc-array` | `declare -A`, `local -A`, `typeset -A`, `readonly -A` | invalid option | 4.0 | NEWS, bash-4.0 |
136
+ | `mapfile` | `mapfile`, `readarray` | command not found | 4.0 | NEWS, bash-4.0 |
137
+ | `case-modification` | `${v,,}` `${v,}` `${v^^}` `${v^}` | bad substitution; ignored on `${a[i],,}` | 4.0 | NEWS, bash-4.0 |
138
+ | `pipe-stderr` | `cmd \|& other` | syntax error | 4.0 | NEWS, bash-4.0 |
139
+ | `append-both` | `cmd &>> file` | syntax error | 4.0 | NEWS, bash-4.0 |
140
+ | `coproc` | `coproc` | command not found | 4.0 | NEWS, bash-4.0 |
141
+ | `nameref` | `declare -n`, `local -n`, `typeset -n` | invalid option | 4.3 | NEWS, bash-4.3 |
142
+ | `param-transform` | `${v@Q}` `@E` `@P` `@A` `@a` / `@U` `@u` `@L` `@K` / `@k` | bad substitution; ignored on `${a[@]@Q}` | 4.4 / 5.1 / 5.2 | NEWS, bash-4.4, bash-5.1, bash-5.2 |
143
+ | `printf-v-array` | `printf -v 'arr[i]'` | not a valid identifier | 4.1 | NEWS, bash-4.1 |
144
+ | `negative-index` | `${arr[-1]}` / `arr[-1]=x` | bad array subscript | 4.2 / 4.3 | NEWS, bash-4.2, bash-4.3 |
145
+ | `negative-length` | `${v:0:-1}`, `${a[0]:1: -2}` | `substring expression < 0` | 4.2 | NEWS, bash-4.2 |
146
+ | `globstar` | `shopt -s globstar` | invalid option name, `**` acts as `*` | 4.0 | NEWS, bash-4.0 |
147
+ | `lastpipe` | `shopt -s lastpipe` | invalid option name | 4.2 | NEWS, bash-4.2 |
148
+ | `wait-n` | `wait -n` | invalid option | 4.3 | NEWS, bash-4.3 |
149
+ | `new-variables` | `$EPOCHSECONDS`, `$EPOCHREALTIME` / `$SRANDOM`, also in `$(( ))` | unset: empty, or unbound under `set -u` | 5.0 / 5.1 | NEWS, bash-5.0, bash-5.1 |
150
+ | `read-i` | `read -i default` | invalid option | 4.0 | NEWS, bash-4.0 |
151
+ | `case-fallthrough` | `;&` and `;;&` | syntax error | 4.0 | NEWS, bash-4.0 |
152
+ | `test-v` | `[[ -v x ]]`, `[ -v x ]`, `test -v x` | syntax error in `[[`, unary operator expected in `[` | 4.2 | NEWS, bash-4.2 |
153
+
154
+ What is not reported, because bash 3.2 handles it: `"$@"` and `"$*"` with no
155
+ arguments, `${#arr[@]}` of an empty array, `${arr[@]:-}`, `${arr[@]+...}`,
156
+ `${!arr[@]}`, `${arr[@]: -1}`, `&>`, `shopt -q globstar`, and any of the above
157
+ inside single quotes, comments, quoted heredocs (`<<'EOF'`) or escaped with a
158
+ backslash. An unquoted heredoc body is expanded by bash, so it is checked. An
159
+ expansion with a default, such as `${EPOCHSECONDS:-$(date +%s)}`, is not
160
+ reported.
161
+
162
+ ## Why Python
163
+
164
+ The check has to tell code from text: `${v,,}` inside single quotes, a comment,
165
+ a quoted heredoc or `$'...'` is fine, and the same characters in an unquoted
166
+ heredoc or inside `$( ... )` nested in double quotes are not. That takes a
167
+ tokenizer that tracks quoting, heredocs, `case` patterns (whose `)` is not the
168
+ end of a `$( )`), `[[ ]]`, arithmetic and command substitution. A grep, awk or
169
+ bash scanner gets these wrong; a real tokenizer is easier to write and test in
170
+ a general-purpose language.
171
+
172
+ Between Go and Python, Python has the least install friction for shell users:
173
+ `pipx install` or `uvx` with no compiler, the `python3` that the Xcode Command
174
+ Line Tools put on every developer Mac (3.9) is enough, pre-commit installs
175
+ `language: python` hooks on its own, and the Action installs into a venv with
176
+ the runner's `python3`. Go would need a Go toolchain for `go install` and for
177
+ pre-commit's `golang` hooks, or per-platform release binaries. The package has
178
+ no runtime dependencies.
179
+
180
+ ## Limits
181
+
182
+ - It tokenizes; it does not build a full syntax tree or follow control flow. It
183
+ cannot know whether an array is empty, so every unguarded `${arr[@]}` in a
184
+ file that enables `set -u` anywhere is reported. Guard it, or suppress the
185
+ line.
186
+ - `set -u` is detected per file: from a `set -u`, `set -o nounset` or a
187
+ `#!/bin/bash -u` shebang. A library sourced by a script that sets it is not
188
+ known to run under it.
189
+ - It does not follow `source`, or look inside strings passed to `eval`,
190
+ `bash -c` or `sh -c`. Backtick substitutions are scanned approximately.
191
+ - Options and command names built at run time (`declare "$flags" x`,
192
+ `"$cmd" -n`) are not seen. Aliases are not expanded.
193
+ - The rule list covers the common breakages, not every bash 4+ feature. Not
194
+ checked yet: `{fd}>` named file descriptors (4.1), `declare -l`/`-u` (4.0),
195
+ `declare -g` (4.2), `read -N` (4.1), `$BASHPID` (4.0), `\u` escapes in
196
+ `$'...'` (4.2), and a `**` glob without the `shopt` line in the same file.
197
+ - `run --docker` mounts only the current directory, and the container has the
198
+ `bash:3.2` image's busybox tools, not macOS's BSD ones. It proves the bash, not
199
+ the rest of the Mac.
200
+
201
+ ## Development
202
+
203
+ ```sh
204
+ uv sync
205
+ uv run ruff check && uv run ruff format --check
206
+ uv run pyright
207
+ uv run pytest
208
+ bash tests/prove_fixtures.sh # every fixture under the bash running it
209
+ docker run --rm -v "$PWD:/work" -w /work bash:4.3 bash tests/prove_fixtures.sh
210
+ ```
211
+
212
+ `tests/fixtures/<rule>/` holds `bad-*.sh` files, each with a `# min-bash:`
213
+ version and the `# expect:` line:column of its findings, and a `good.sh` with
214
+ look-alikes that must not be reported: the same text in quotes, comments and
215
+ quoted heredocs, and the bash 3.2 way to do the same thing. `prove_fixtures.sh`
216
+ runs them all under the bash that runs it: on a Mac, the tests run it with
217
+ `/bin/bash` 3.2, and CI runs it in the `bash:3.2` to `bash:5.3` images.
218
+
219
+ ## License
220
+
221
+ MIT
@@ -0,0 +1,46 @@
1
+ name: bash32-lint
2
+ description: Find bash constructs that break on macOS's /bin/bash 3.2, and optionally run a script under bash 3.2.
3
+ branding:
4
+ icon: terminal
5
+ color: gray-dark
6
+
7
+ inputs:
8
+ paths:
9
+ description: Space-separated scripts or directories to check.
10
+ default: "."
11
+ ignore:
12
+ description: Comma-separated rule ids to skip.
13
+ default: ""
14
+ run:
15
+ description: >-
16
+ A script to also run under bash 3.2, such as a test runner. Uses /bin/bash on macOS runners and the
17
+ bash:3.2 image elsewhere. Empty to skip.
18
+ default: ""
19
+
20
+ runs:
21
+ using: composite
22
+ steps:
23
+ - name: Install bash32-lint
24
+ shell: bash
25
+ run: |
26
+ python3 -m venv "$RUNNER_TEMP/bash32-lint"
27
+ "$RUNNER_TEMP/bash32-lint/bin/python" -m pip install --quiet --disable-pip-version-check "$GITHUB_ACTION_PATH"
28
+
29
+ - name: Check for bash 4+ constructs
30
+ shell: bash
31
+ env:
32
+ PATHS: ${{ inputs.paths }}
33
+ IGNORE: ${{ inputs.ignore }}
34
+ run: |
35
+ set -f
36
+ args=(--format github)
37
+ if [ -n "$IGNORE" ]; then args+=(--ignore "$IGNORE"); fi
38
+ # PATHS is split on spaces on purpose; globbing is off.
39
+ "$RUNNER_TEMP/bash32-lint/bin/bash32-lint" check "${args[@]}" $PATHS
40
+
41
+ - name: Run under bash 3.2
42
+ if: inputs.run != ''
43
+ shell: bash
44
+ env:
45
+ RUN_SCRIPT: ${{ inputs.run }}
46
+ run: '"$RUNNER_TEMP/bash32-lint/bin/bash32-lint" run "$RUN_SCRIPT"'