More Bash answers

by Danny
Published 3 min read

Use BASH_SOURCE with dirname, cd and pwd to get the absolute directory of the running script, so it can find its own files no matter which directory it was started from.

The short answer

#

Combine BASH_SOURCE, dirname, cd and pwd inside a command substitution. This gives you an absolute path to the folder that holds the script, even if it is called from somewhere else or sourced.


_10
script_dir="$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")" &> /dev/null && pwd)"
_10
_10
source "$script_dir/lib/helpers.sh"

Why it happens

#

Relative paths in a script are resolved against the current working directory, which is wherever the user was when they ran it, not where the script lives. A script that reads ./config.json works when you run it from its own folder and breaks as soon as you run it from anywhere else, for example from cron or CI.

The obvious fix, dirname "$0", is not quite right either:

  • $0 is the name the script was invoked with, so it is often a relative path such as ./deploy.sh or scripts/deploy.sh. dirname then returns . or scripts, which is still relative.
  • When a script is sourced with source or ., $0 is the name of the shell, such as bash, not the file.

BASH_SOURCE is an array Bash keeps of the source files on the current call stack. ${BASH_SOURCE[0]} is the file containing the code that is running right now, whether it was executed or sourced. Running cd into its directory and then pwd turns that into an absolute path.

How to fix it

#

Use the standard one-liner

Here is what each part of the short answer does:

  • ${BASH_SOURCE[0]} is the path to this script file.
  • dirname -- strips the file name and leaves the folder. The -- stops a path starting with a dash being read as an option.
  • cd -- moves into that folder inside the command substitution, which runs in a subshell, so your script's own working directory does not change.
  • &> /dev/null hides anything cd prints. If the CDPATH variable is set, cd echoes the new directory, which would otherwise end up in your variable.
  • && pwd prints the absolute path, but only if cd succeeded.

The double quotes around every expansion keep it working when the path contains spaces.

If the script is run through a symlink, for example /usr/local/bin/deploy pointing at ~/tools/deploy.sh, the one-liner gives you the folder of the symlink. To get the folder of the real file, resolve the path first with realpath:


_10
script_path="$(realpath -- "${BASH_SOURCE[0]}")"
_10
script_dir="$(dirname -- "$script_path")"

realpath is part of GNU coreutils on Linux and is included in recent versions of macOS. Check with command -v realpath if your script needs to run on older systems.

If you only want to resolve symlinked directories, not the script itself, use pwd -P instead of pwd in the one-liner.

Build every path from script_dir rather than changing directory, so that relative paths the user passes as arguments still mean what they expect.


_10
config="$script_dir/config.json"
_10
source "$script_dir/lib/helpers.sh"
_10
"$script_dir/bin/tool" --input "$1"

If you really do want the script to run from its own folder, cd "$script_dir" || exit 1 near the top is fine, but resolve any path arguments before you do it.

In POSIX sh

BASH_SOURCE only exists in Bash. In a #!/bin/sh script, the closest you can get is $0, which works when the script is executed but not when it is sourced.


_10
script_dir="$(cd -- "$(dirname -- "$0")" && pwd)"

Common pitfalls

#
  • Leaving out the quotes, so the path splits apart on the first space.
  • Using $0 in a file meant to be sourced, which gives you the shell's name instead.
  • Using dirname on its own, which returns a relative path such as ..
  • Forgetting about CDPATH, which can add an extra line to the captured path.
  • Running the script as curl ... | bash. There is no file in that case, so there is no directory to find.
  • Using BASH_SOURCE inside a script run by sh, where it is simply empty.
#

Was this resource helpful?