Unix shell script find out which directory the script file resides?

William Yeung picture William Yeung · Oct 28, 2008 · Viewed 567.6k times · Source

Basically I need to run the script with paths related to the shell script file location, how can I change the current directory to the same directory as where the script file resides?

Answer

TheMarko picture TheMarko · Oct 28, 2008

In Bash, you should get what you need like this:

#!/usr/bin/env bash

BASEDIR=$(dirname "$0")
echo "$BASEDIR"