shellcheck icon indicating copy to clipboard operation
shellcheck copied to clipboard

Infer shell from filename for shell-specific files

Open benblank opened this issue 3 years ago • 3 comments

  • Rule Id: SC2148
  • [ ] https://www.shellcheck.net/ (i.e. the latest commit) currently gives no useful warnings about this (N/A — ShellCheck.net doesn't involve filenames)
  • [x] I searched through https://github.com/koalaman/shellcheck/issues and didn't find anything related

I happened to pop open my .bashrc in VS Code code today and noticed the "squiggles" indicating a ShellCheck error. Sure enough, SC2148 was triggering on the first line of the file, as there's no shebang, no shell directive, and no ….bash extension.

But wouldn't it be nice if ShellCheck could infer the shell for files which are particular to that shell? (e.g. .kshrc, .bash_profile)

I can think of a few ways to accomplish this:

  1. Infer any file whose name starts with .bash/.dash/.ksh to have the corresponding shell.
    Pros
    • (Hopefully) easy to implement.
    • Easy to understand.
    Cons
    • Has the potential for both false positives and false negatives.
    • Totally inflexible from the user's perspective; either a file has a matching name or it does not.
  2. A fixed mapping of filename -> shell.
    Pros
    • (Almost) no chance for false positives.
    Cons
    • Adds the maintenance burden of keeping the mapping up-to-date.
    • Totally inflexible from the user's perspective; either a file has a matching name or it does not.
    • Higher implementation cost than above.
  3. A configurable mapping of filename -> shell.
    Pros
    • (Almost) no chance for false positives.
    • Configurable; users may use any set of mappings they desire.
    Cons
    • (If a default mapping is provided by ShellCheck.) Adds the maintenance burden of keeping the mapping up-to-date.
    • Highest implementation cost of the three.

Do any of these options sound reasonable to add to ShellCheck?

Here's a snippet or screenshot that shows the problem:

Given the following file as ~/.bashrc:

export BASH_SILENCE_DEPRECATION_WARNING=1
export EDITOR=nano
export HISTIGNORE="&"
export PATH=$HOME/bin:/usr/local/bin:$PATH

eval "$(keychain --agents ssh,gpg ssh_identity gpg_key_id)"

Here's what shellcheck currently says:

In .bashrc line 1:
export BASH_SILENCE_DEPRECATION_WARNING=1
^-- SC2148 (error): Tips depend on target shell and yours is unknown. Add a shebang or a 'shell' directive.

For more information:
  https://www.shellcheck.net/wiki/SC2148 -- Tips depend on target shell and y...

Here's what I wanted to see:

No errors.

benblank avatar Oct 20 '22 22:10 benblank

100% Agree.

Alternatively this could be implemented in the shellcheck control notation along side # shellcheck disable....

Thanks for the great utility. Keep it up!

AlexAtkinson avatar Nov 07 '25 22:11 AlexAtkinson

Heh, I'd almost forgotten about this feature request. 🙂

While I still think this kind of auto-detection would be nice to have, lacking it turns out to really not be that much of a paper-cut. Because I don't find myself creating new files of this type very often, just sticking e.g. # shellcheck shell=bash at the top feels like perfectly reasonable UX.

benblank avatar Nov 08 '25 04:11 benblank

Alternatively this could be implemented in the shellcheck control notation along side # shellcheck disable....

isn't this already the case? Via a directive set the type of shell.

# shellcheck shell=sh

https://github.com/koalaman/shellcheck/wiki/Directive#shell

brother avatar Nov 10 '25 07:11 brother