Possible Duplicate:
Reliable way for a bash script to get the full path to itself?
I have bash script test.sh
which use functions from another search.sh
script by following lines:
source ../scripts/search.sh
<call some functions from search.sh>
Both scripts are located in git repository. search.sh
in <git_root>/scripts/
directory, test.sh
is located in the same directory (but, generally speaking, could be located anywhere inside <git_root>
directory - I mean I can't rely on the following source search.sh
approach ).
When I call test.sh
script from <git_root>/scripts/
everything works well, but as soon as I change current working directory test.sh
fails:
cd <git_root>/scripts/
./test.sh //OK
cd ..
./scripts/test.sh //FAILS
./scripts/test.sh: line 1: ../scripts/search.sh: No file or directory ...
Thus what I have:
- Relative path of
search.sh
script towards<git_root>
directory
What I want: To have ability to run test.sh
from anywhere inside <git_root>
without errors.
P.S.: It is not possible to use permanent absolute path to search.sh
as git repository can be cloned to any location.
test.sh
script. – likern Jan 9 '13 at 15:02source
a relative path from the current directory, sourcing a relative path from the script's path seems like a good improvement. The only complication is if the user moves the script to a different location altogether, but I cannot imagine how you would fix that, other than with an environment variable. – tripleee Jan 9 '13 at 16:07search.sh
script is in the same directory astest.sh
, then finding the path thattest.sh
is in will most definitely help, as then you use that path to find your other script. – Some programmer dude Jan 9 '13 at 16:55