There is no solution that will work equally well in all shell options.
In bashyou can use BASH_SOURCE:
$(dirname "$BASH_SOURCE")
Example:
$ cat /tmp/1.sh
. /tmp/sub/2.sh
$ cat /tmp/sub/2.sh
echo $BASH_SOURCE
$ bash /tmp/1.sh
/tmp/sub/2.sh
, script 2.sh,
/tmp/1.sh, 2.sh source.
, bash. Bourne-shell (/bin/sh) .
csh/tcsh/zsh $_ BASH_SOURCE.