Ask questionsSC1090/SC1091: sourcing should find files relative to script

When using the directive, then it seems to look up the source relative to the path shellcheck is being run from. For example, I have the following scripts: and in the same directory (for example bar/baz from the project root).

Suppose there is the following line in

source "${0%/*}/"

I will get a SC1090 error because shellcheck is unable to resolve the path. I have to do this:

# shellcheck source bar/baz/
source "${0%/*}/"

This works, but only if I run the checker from the project root (either by specifying foo/bar/ as an input file, or with -x), but not if I run it from anywhere else. There should be a way to tell shellcheck to look for a file relative to the checked file's location, not where shellcheck is run. For example, this way:

# shellcheck source ./
source "${0%/*}/"

Answer questions iyerusad

I recently added shellcheck to my workflow within VS Code (via this extension). As such, shellcheck is called per file. Very helpful in IDE, I tend to jump between several languages/syntax in any given day - Thank you.

In my limited view, I'm thinking #shellcheck source= directive should attempt to be relative by default:

  1. If I'm specifying a shellcheck source directive, it means higher up helpers (such as recent have been unsuccessful (or are undefined)
  2. shellcheck executable location not likely same as script being checked.

My particular instance of SC1090 is (where dotfiles is a repo):

if [ -f "$HOME/dotfiles/1" ];then
  . "$HOME/dotfiles/1"

It would be nice if could handle case of $HOME - I rather avoid helper directives in code unless explicitly breaking a rule for a really good reason.


Related questions

SC2181 suggests to check exit code directly when I check for several possible values hot 1
Github User Rank List