An official standard / convention for a file extension for shell scripts to source

I'd use .sh (for files in the POSIX sh language, .bash for non-sh-compatible bash files, that is the extension identifies the language the script is written in) for files intended to be sourced (or more generally not intended to be executed), and no extension for files that are meant to be executed.

You can also add a:

#! /bin/echo Please-source

she-bang, so that when executed by mistake (though I'd expect those files should not be given execution permissions, which would already prevent execution), you get a notice that it should be sourced instead.


In case of source files, i think the best way is .conf for files that configure your script and .shlib or .shlibs for files that have functions or other utils.

If you want to prevent your script to run with the wrong shell, and hashbang is not enough for you, you can use this:

if [ "$(readlink "/proc/$$/exe")" != "/bin/bash" ]; then
      echo >&2 "CAUTION: Wrong interpreter detected. You must use bash."
      exit 1
fi