bash: get the directory this script is in

${BASH_SOURCE%/*}

This is useful when you want to read a file that’s in the same directory as the current script, for instance.

$BASH_SOURCE holds the current script, including a path. That could be a relative path or a full path.

% says “strip off a glob from the end of the value, the smallest glob that matches”

/* is a glob, which will match the / at the end of the file’s directory plus the filename.

So this expression gives you the directory of the bash script you’re currently in.

This trick brought to you by Avdi.