BASH_SOURCE

noun

say it: “BASH SORS” /ˈbæʃ ˈsɔrs/

A bash variable that holds the path of the file bash is reading, so a script can find out where it is saved.

What is BASH_SOURCE in bash?

BASH_SOURCE is a variable bash fills in with the path of the file it's reading. It holds a list, and ${BASH_SOURCE[0]} is the script running now. With dirname, it gives the script's own folder, so the script finds files saved beside it from any folder. zsh doesn't have it.

# where.sh, run as ~/terminal-practice/shuffler/where.sh
echo "This file: ${BASH_SOURCE[0]}"
echo "Its folder: $(dirname "${BASH_SOURCE[0]}")"
# This file: /home/sam/terminal-practice/shuffler/where.sh
# Its folder: /home/sam/terminal-practice/shuffler

In a sentence

  • “BASH_SOURCE is bash's own, so the trick doesn't work in a zsh script.”
  • “With dirname and BASH_SOURCE, stats.sh finds mix-lib.sh beside it, whatever folder Sam runs it from.”

Level: Journeyman · More Unix terms at this level

Updated