Home > Bash, Linux > How To Determine Directory of Sourced Bash Script

How To Determine Directory of Sourced Bash Script

September 2nd, 2009 Masao Leave a comment Go to comments

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]%/*}

Categories: Bash, Linux Tags:
  1. No comments yet.
  1. No trackbacks yet.