How to find script directory in an included shell script

Viewed 217

We now to find the directory of a shell script using dirname and $0, but this doesn't work when the script is inluded in another script.

Suppose two files first.sh and second.sh:

/tmp/first.sh :

#!/bin/sh
. "/tmp/test/second.sh"

/tmp/test/second.sh :

#!/bin/sh
echo $0

by running first.sh the second script also prints first.sh. How the code in second.sh can find the directory of itself? (Searching for a solution that works on bash/csh/zsh)

1 Answers
Related