Is there a way for a sourced shell script to find out the path to itself? I'm mainly concerned with bash, though I have some coworkers who use tcsh.
I'm guessing I may not have a ton of luck here, since sourcing causes commands to be executed in the current shell, so $0
is still the current shell's invocation, not the sourced script. My best thought currently is to do source $script $script
, so that the first positional parameter contains the necessary information. Anyone have a better way?
To be clear, I am sourcing the script, not running it:
source foo.bash
Best Answer
In
tcsh
,$_
at the beginning of the script will contain the location if the file was sourced and$0
contains it if it was run.In Bash: