Are you talking about only if you manually source a file into the shell? I imagine so, because otherwise you'd have that path already encoded into the script that does the sourcing.
I can't think of any way personally to do it from inside the sourced file outside of hardcoding it, since as you've discovered it effectively becomes part of the shell/script when sourced. AFAICT, you'll have to set it manually from the outside.
You can use readlink -f
to get the absolute path to it though.
$ export MyOwnLocation="$(readlink -f ../../someDir/sub.bashrc)"
$ source "$MyOwnLocation"
If the sourced file is always located in a path relative to the location of the original shell/script, as opposed to the pwd, you could use readlink to get the absolute path of that, then use it to build the path to the sourced file. For example:
shellloc="$(readlink -f $0)"