How to Get the Directory of a Bash Script

Have you ever tried to get the directory of your Bash script programmatically?

It’s a common thing people ask and it can be done in different ways.

The first thing to look at to solve this problem is the $0 variable, used in Bash to store the first element of the command executed.