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 . a.sh, a.sh‘s $0 does not give a.sh 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.

Bash and Shell

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

From bash manual:

BASH_SOURCE
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: a.sh and b.sh which is sourced by a.sh and contains a function.

a.sh

#!/bin/bash

echo "\$0: $0"

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

. ./b.sh

b.sh

#!/bin/bash

echo "\$0: $0"

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

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

fun

What happens if we run a.sh directly by sh a.sh?

The result is as follows.

$0: a.sh
main in a.sh
$0: a.sh
source in ./b.sh
fun in ./b.sh
a.sh

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

$0: bash
source in a.sh
$0: bash
source in ./b.sh
fun in ./b.sh
bash

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 Zhiqiang Ma

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.

Leave a Reply

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