Home > Net >  How can I get the current script name of a script running in parent script's shell?
How can I get the current script name of a script running in parent script's shell?

Time:07-08

How can I get the name of a child script that is running in the shell of it's parent?

consider script1.sh for example;

#this is script 1
echo "$0"
. ./script2.sh

and script2.sh;

#this is script 2
echo "$0"

Since script2.sh is being executed in the same shell as script1.sh, the output of running these scripts are both;

./script1.sh

How can get the name of script2.sh withing script2.sh?

CodePudding user response:

Would this be what you are looking for? After a quick google search this is what I found. https://unix.stackexchange.com/questions/620191/getting-a-parent-script-name-from-a-child-script

CodePudding user response:

You are sourcing a script, not running it. Therefore, you are looking for BASH_SOURCE.

$ cat test.sh 
echo $BASH_SOURCE

$ . ./test.sh 
./test.sh
  • Related