sh icon indicating copy to clipboard operation
sh copied to clipboard

feat: shfmt detects shell type from inline '# shellcheck shell=...'

Open gzm55 opened this issue 8 months ago • 3 comments

some source-able shell scripts use # shellcheck shell=... to select script dialect instead of a shebang. can we also support this anotation?

gzm55 avatar Apr 09 '25 07:04 gzm55

Where is this documented?

mvdan avatar Apr 09 '25 07:04 mvdan

https://www.shellcheck.net/wiki/Directive (search 'shellcheck shell') https://github.com/koalaman/shellcheck/blob/master/shellcheck.1.md?plain=1#L100

gzm55 avatar Apr 09 '25 08:04 gzm55

Husky (popular in the NodeJS world) for example, does neither require its hooks to be executable nor to have a shebang.

$ mkdir -p /tmp/test/.husky && cd "$_/.."
$ git init -q

$ npm init -y >/dev/null
$ npm i -D --silent husky
$ echo 'exit 70' >.husky/pre-commit
$ npx husky

$ ls -la .husky/pre-commit
-rw-------  1 redacted  wheel  7 May 11 13:38 .husky/pre-commit
$ git config core.hooksPath
.husky/_
 
$ git add --all -- ':!node_modules'
$ git status -s -uno
A  .husky/pre-commit
A  package-lock.json
A  package.json

$ git commit -m initial
$ echo $?
1
$ git status -s -uno
A  .husky/pre-commit
A  package-lock.json
A  package.json

$ echo 'exit 0' >.husky/pre-commit
$ git commit -m initial
[main (root-commit) c45d1d6] initial
 3 files changed, 48 insertions(+)
 create mode 100644 .husky/pre-commit
 create mode 100644 package-lock.json
 create mode 100644 package.json

https://github.com/typicode/husky/releases/tag/v9.1.1

#!/usr/bin/env sh and . "$(dirname -- "$0")/_/husky.sh" are deprecated. husky command will automatically remove them, no action required.

sdavids avatar May 11 '25 11:05 sdavids

Another use case: zsh functions

zfunc/some_func

some_func() { print This is func; }

zfunc/some_other_func

some_other_func() { print This is another func; }

This will ignore the two functions above:

$ shfmt -l zfunc

This works

$ find zfunc -type f -print0 | xargs -0L1 shfmt -l

… until you hit "xargs: argument line too long"


It would be nice if there were a marker comment so that shfmt does not ignore them.

sdavids avatar Jul 08 '25 18:07 sdavids