Why does shellcheck fail when a source file is representing the variables at the top of a script?

Shellcheck is a static analysis tool. It does not handle dynamic paths (based on variables, or expressions). As an alternative, consider adding the source directive.

Example:

# shellcheck source=./lib.sh
source "$(find_install_dir)/lib.sh"

The source= directive tells shellcheck the location of the dynamically generated filename. From the question, this should be the (most likely, relative) location of the .sourcefile.

Documented in https://github.com/koalaman/shellcheck/blob/master/shellcheck.1.md


I use shellcheck 0.4.4 and it also says:

In start.sh line 2: . "$(pwd)"/.sourcefile
^-- SC1090: Can't follow non-constant source. Use a directive to specify location.

That means you'll have to use absolute paths, but newer versions might support -P.

If you switched to a non-constant source, it will not work as well because .sourcefile needs a shebang and you have to add this file with -x as such:

shellcheck -x .sourcefile start.sh

It still will complain about:

day="Monday"
^-- SC2034: day appears unused. Verify it or export it.