Commit e7a9af9
committed
Updated shebang & extention regex to be safer & match valid shellcheck shells
* `sh`
* `ash`
* `dash`
* `bash`
* `bats`
* `ksh`
[src](https://github.com/koalaman/shellcheck/blob/c2a15ce8e906ae6a12bcbe32eac7ac586fbcb59b/src/ShellCheck/Parser.hs#L3186-L3193)
---
Shebang regex:
Was: `'^#!.*sh'`
Now: `'^#!\(/\|/.*/\|/.* \)\(\(ba\|da\|k\|a\)*sh\|bats\)$'`
Test cases:
https://pastebin.com/BKB92wx2
Previous regex matched a lot of invalid shebangs & missed the 6 valid `bats` shebangs.
Current regex matches just the 6 correct shebangs from each valid shell.
There are probably some cases that I can't think of but it's a lot safer than the existing one.
A compromise would be something like: `'^#!/.*\(\(ba\|da\|k\|a\)*sh\|bats\)$'`
It matches the `zsh` `fish` and extra characters + valid shell ones but they're all at least valid.
---
File ending regex:
Was: `\.sh$|bash$`
Now: `.+\.(sh|bash|dash|ksh|ash|bats)$`
Test cases:
https://pastebin.com/JRJg8N13
Testing script:
```
test() {
if [[ "$1" =~ .+\.(sh|bash|dash|ksh|ash|bats)$ ]]; then
echo "match - $1"
else
echo "not match - $1"
fi
}
while IFS= read -r FILE; do
if [[ "$FILE" != "" ]]; then
test "$FILE"
fi
done <<EOF
TEST CASES HERE
EOF
```
Previous regex would match `.sh` `bash` and miss `file.ksh` `file.dash` `file.ash` `file.bats`
Current regex only matches the 3 normal file names + also matches just `.sh` `.bash` etc, which are very unusual but valid file names.
There's no extra checking for slashes etc because file names & escapes could confuse things, just the extension should be enough.1 parent 561039a commit e7a9af9
1 file changed
+3
-2
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
36 | 36 | | |
37 | 37 | | |
38 | 38 | | |
39 | | - | |
| 39 | + | |
| 40 | + | |
40 | 41 | | |
41 | 42 | | |
42 | 43 | | |
43 | | - | |
| 44 | + | |
44 | 45 | | |
45 | 46 | | |
46 | 47 | | |
| |||
0 commit comments