How To Determine Directory of Sourced Bash Script

Sourcing a script can be useful for exporting variables in the current shell.

However, you can’t use “dirname $0″ to get the directory of the script.

Here’s how to get the same effect of dirname, but still allowing you to export variables.

Relative path:

DIRNAME=${BASH_ARGV[0]%/*}

Full path:

ABSDIR=$PWD/${BASH_ARGV[0]%/*}

This entry was posted in Bash, Linux. Bookmark the permalink.

Leave a Reply

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

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>