Construct script-relative paths in bash

Sometimes you want to refer something relative to the script. This can be done with the BASH_SOURCE magic variable:

# This will print "some-file" relative to the current script.
echo "$(< ${BASH_SOURCE%/*}/some-file)"

Since BASH_SOURCE contains the full path to the script, the %/* part replaces the last / character and everything after it. Keep in mind this won’t work if you do sh something.sh from the same directory, as there won’t be any slashes in BASH_SOURCE.

Thank you, Stack Overflow!