How to get the script’s own path in sourced Bash script?

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. How to get the a.sh inside a.sh?

Short answer: use ${BASH_SOURCE[0]}.

You can check this post for details and explanations: How to Get Bash Script’s Own Path.

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.

Leave a Reply

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