Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

In /usr/local/bin I have a 'ln -s' to /usr/local/foo/bash.script, and in this latter script I want to know the current /usr/local/foo directory, so that I can run a secondary script from that folder.

Now, I tried dirname $0, but that gives me the /usr/local/bin folder instead. What should I be using in the bash.script to get the /usr/local/foo folder?

share|improve this question
up vote 5 down vote accepted

try readlink, e.g.


while readlink $d >/dev/null; do
  d=`readlink $d`
echo $d


I am not sure if that works on other Unix OS, but if you are on linux the above can be simplified by using

d=`readlink -f $0`

echo $d


readlink --help
share|improve this answer
Thanx for the pointers! I still need to combine with dirname, and I saw the readlink -f, doing the above in one command ... any idea on how portable that is? – Egon Willighagen Jan 31 '10 at 12:03
@Egon: -f worked for on cygwin (on Win-7-64bit) too. But it doesn't work on MacOS. – Vokuhila-Oliba Jan 31 '10 at 12:10
Ah, well, that's good enough for my needs, because it is part of a script only for Linux :) thanx! – Egon Willighagen Jan 31 '10 at 12:13

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.