How to Get Bash Script’s Own Path

Bash script may need to get its own path. In normal Bash script, $0 is the path to the script. However, when a script is sourced, such as .,‘s $0 does not give while the caller’s name. How to reliably get a bash script’s own path no matter whether the Bash script is executed or sourced is introduced in this post.

Short answer

You can use ${BASH_SOURCE[0]} to get the script’s own path, in sourced or directly executed Bash script.

Why ${BASH_SOURCE[0]} contains the own path

From bash manual:

An array variable whose members are the source filenames where the corresponding shell function names in the FUNCNAME array variable are defined. The shell function ${FUNCNAME[$i]} is defined in the file ${BASH_SOURCE[$i]} and called from ${BASH_SOURCE[$i+1]}.

What about ${BASH_SOURCE[0]? This following example shows it well.

We have 2 Bash scripts: and which is sourced by and contains a function.


echo "\$0: $0"

echo ${FUNCNAME[0]} in ${BASH_SOURCE[0]}

. ./


echo "\$0: $0"

echo ${FUNCNAME[0]} in ${BASH_SOURCE[0]}

fun() {
    echo ${FUNCNAME[0]} in ${BASH_SOURCE[0]}
    echo $0


What happens if we run directly by sh

The result is as follows.

main in
source in ./
fun in ./

What about we source by . instead of executing it? It gives

$0: bash
source in
$0: bash
source in ./
fun in ./

In summary, in sourced or directly executed bash script files, ${BASH_SOURCE[0] contains the path of the bash source file and ${FUNCNAME[0] contains “source” or “main”. So, ${BASH_SOURCE[0]} gives you a reliable variable for the Bash script source file path no matter whether it is used inside of a function and no matter whether the script is directed executed or sourced.

Eric Ma

Eric is a systems guy. Eric is interested in building high-performance and scalable distributed systems and related technologies. The views or opinions expressed here are solely Eric's own and do not necessarily represent those of any third parties.


    //A c function to get your own executable's execution directory. 
    // Sometimes you want to know the program source 
    #ifndef MAX_PATH
    #define MAX_PATH 256
    static char * get_path_to_me(void);
    static char *
    get_path_to_me ()
        static char buffer[MAX_PATH];
        char *cp;
        int len;
        if ((len = readlink ("/proc/self/exe", buffer, sizeof (buffer) - 1)) != -1)
            buffer[len] = '\0';
            fprintf (stderr, "\n%s version 0.9, date Compiled: %s, ", cp+1, __DATE__);
            fprintf (stderr, "is executed from %s\n", buffer);      //program executes, dirname ok
            *buffer = '\0';
        return (buffer);
    #if 0             // set to 1 to test as stand-alone, leave  as 0 if you want to include this function in your code.
     * @brief main
     * @param argc
     * @param argv
     * @return
    main (int argc, char *argv[])
        char *pgm;
        char *pgmx;
        pgm = strdup (get_path_to_me ());
        printf("\nfull path = %s, directory=%s program=%s\n",buffer,dirname(pgm),basename(pgm));
        return 0;
    1. Thanks for sharing the C code.

      This is a nice trick:

      readlink (“/proc/self/exe”, buffer, sizeof (buffer) – 1)

  2. This is awesome.

    I still don’t completely understand it,
    but I definitely understand it better than before
    and I have a lot to learn about bash fundamentals anyway.

    Thanks for the write-up!

Leave a Reply

Your email address will not be published. Required fields are marked *