Skip to main content

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

Required fields*

7
  • Works perfectly and is way simpler than the stuff I was trying. Thanks! Commented Jul 27, 2011 at 20:19
  • @MikeDeck Enjoy. Note that I fixed some quoting issues since first posting ... you might use an option from the latest edit in the event that you have spaces or other wacky characters in your path sometime. Even if it doesn't matter for this project, you might copy the code from your own scripts down the road and having the best option matters then! Commented Jul 27, 2011 at 20:40
  • 3
    I should clarify, the second one works perfectly. The first solution you gave does not work for symlinks on my system. Commented Jul 27, 2011 at 20:47
  • 6
    @MikeDeck: The first solution works for the typical case of having various directories symlinked as part of the path, but won't help if the final binary is a symlink. That's what the second one manages to take care of. Commented Jul 27, 2011 at 21:19
  • 5
    Doesn't work if the script file is sourced (e.g $ source ./sript.sh) This works for both cases: DIR="$(cd "$(dirname "$BASH_SOURCE")" && pwd)" Commented Sep 6, 2018 at 18:18