Referring to a file relative to executing script


Bash Problem Overview

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, while the script to be sourced is, so in I have:


However this only works when running in the directory containing it, since 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.

Bash Solutions

Solution 1 - Bash

See:">BASH FAQ entry #28: "How do I determine the location of my script? I want to read some config files from the same place."

Any solution isn't going to work 100% of the time:

> It is important to realize that in the general case, this problem has no solution. Any approach you might have heard of, and any approach that will be detailed below, has flaws and will only work in specific cases. First and foremost, try to avoid the problem entirely by not depending on the location of your script!

If you need to write a very reusable tool, then taking the correct path as a parameter to your script is going to be the most reliable method.

Assuming your script is only going to be run from certain shells, and only with a little bit of flexibility required, you can probably relax some of this paranoia. It is still good to look at your options. There are common patterns that people use that are particularly problematic.

In particular, the FAQ recommends avoiding the very commonly used $0 variable:

> Nothing that reads $0 will ever be bulletproof, because $0 itself is unreliable.

As an alternative, you could use $BASH_SOURCE instead. Something like this:

source "${BASH_SOURCE%/*}/"

There are some caveats to this solution, too. Check out the FAQ page to see the trade-offs between different solutions. They seem to recommend cd in combination with $BASH_SOURCE in cases where it will work for you, as you get a handy error condition when it fails to expand properly.

Solution 2 - Bash

See this:

I suggest to use:

source $(dirname $0)/

Solution 3 - Bash

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]}")"

Solution 4 - Bash

Try the following:

source ${BASH_SOURCE[0]/}


All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionRyan LiView Question on Stackoverflow
Solution 1 - BashIgnacio Vazquez-AbramsView Answer on Stackoverflow
Solution 2 - BashvagovszkymView Answer on Stackoverflow
Solution 3 - BashGayan WeerakuttiView Answer on Stackoverflow
Solution 4 - BashCluelessView Answer on Stackoverflow