The Right Way to Get the Directory of a bash Script

By Wednesday, October 8, 2014 0 , , , Permalink 4

When writing bash scripts, you might want to get the directory that contains your script. There are multiple ways to accomplish that. Due to the flexibility of bash, some solutions work in some cases, but not in others.

In this post, I evolve from a naive solution to a robust and consistent solution for this common problem. Spoiler – a “good enough” middle ground that I often use is "$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )", as long as I know that symbolic links are out of the game.

Continue Reading…