Here is the scenario:
foo/ > data/ > stuff/ > scripts/ -> /.../generic/scripts
foo/scripts/bar.sh from any location (other folder or inside the folder), I would like to be able for the script to use
However, if I simply use
foo_dir='dirname $0'/.. then it'll go down the directory pointed by the symbolic link! (And obtain
generic instead of
If I use some pattern to eat up the end of
$0, then it's not working either since you could also call the script from inside the directory.
So, in the end... The only thing I have found is this extremely ugly stuff:
dir=`dirname $0` cd $dir dir=`pwd | sed -r 's_/[^/]+$__'` cd -
...but I'm sure there is a better way, isn't there?
What I used:
dir=$( dirname $( cd `dirname $0`; pwd ) )
Dunno if it is perfect but it seems to behave as expected.