Skip to content

Commit e7a9af9

Browse files
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

File tree

1 file changed

+3
-2
lines changed

1 file changed

+3
-2
lines changed

hooks/shellcheck.sh

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,11 +36,12 @@ parse_arguments() {
3636
parse_arguments "$@"
3737

3838
for FILE in $files; do
39-
if (head -1 "$FILE" | grep '^#!.*sh' >/dev/null); then
39+
SHEBANG_REGEX='^#!\(/\|/.*/\|/.* \)\(\(ba\|da\|k\|a\)*sh\|bats\)$'
40+
if (head -1 "$FILE" | grep "$SHEBANG_REGEX" >/dev/null); then
4041
if ! shellcheck ${enable_list:+ --enable="$enable_list"} "$FILE"; then
4142
exit_status=1
4243
fi
43-
elif [[ "$FILE" =~ \.sh$|bash$ ]]; then
44+
elif [[ "$FILE" =~ .+\.(sh|bash|dash|ksh|ash|bats)$ ]]; then
4445
echo "$FILE: missing shebang"
4546
exit_status=1
4647
fi

0 commit comments

Comments
 (0)