I have a utility consisting of a couple of directories with some bash scripts and supporting files that will be deployed to several machines, possibly in a different directory on each machine. The scripts need to be able to reference paths relative to themselves, so I need to be able to get the path to the file that's currently being executed.
I am aware of the dirname $0
idiom which works perfectly when my script is called directly. Unfortunately there is a desire to be able to create symlinks to these scripts from a totally different directory and still have the relative pathing logic work.
An example of the overall directory structure is as follows:
/
|-usr/local/lib
| |-foo
| | |-bin
| | | |-script.sh
| | |-res
| | | |-resource_file.txt
|-home/mike/bin
| |-link_to_script (symlink to /usr/local/lib/foo/bin/script.sh)
How can I reliably reference /usr/local/lib/foo/res/resource_file.txt
from script.sh
whether it is invoked by /usr/local/lib/foo/bin/script.sh
or ~mike/bin/link_to_script
?