Referring to a file relative to executing script

Viewed 80672

In a bash script I'm writing, I use source to include the variable defined in a configuration file. The script to be executed is act.sh, while the script to be sourced is act.conf.sh, so in act.sh I have:

source act.conf.sh

However this only works when running act.sh in the directory containing it, since act.conf.sh there refers to the file placed under the working directory. Is there a solution to make it refer to the file relative to the executing script without invoking cd? Thanks.

4 Answers

Add this on top of the script. Then, all the subsequent code will be get executed relative to the location of the script.

cd "$(dirname "${BASH_SOURCE[0]}")"
Related