Here is the scenario:
foo/
> data/
> stuff/
> scripts/ -> /.../generic/scripts
When calling foo/scripts/bar.sh from any location (other folder or inside the folder), I would like to be able for the script to use foo/data and foo/stuff
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 foo!)
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.

..point back to the wrong place! And avoiding having to resort to regexes to solve such a simple issue. – arnaud Jun 6 '11 at 16:13