In Bash, the correct way to get the full path to a shell script is ${BASH_SOURCE[0]}
, since $0
returns bash
if the script is being sourced. For example, try running this in your terminal:
source <(echo 'echo $0')
It will print bash
.
Does anyone know how to do this for Bourne shell scripts? For example, what would be the contents of a script foo
such that if I typed this:
sh -c '. foo'
it would output the full path to the script?