Link to home
Start Free TrialLog in
Avatar of TerryNJones
TerryNJones

asked on

Determine the name of a calling Bash script .

I have a Bash setup script which is called by many other scripts.  For example I have a script named myScript1 that looks like:

#!/bin/bash
. ./setup.sh

How can setup.sh determine the name of the script that called it?
Thanks - Terry
Avatar of ozo
ozo
Flag of United States of America image

If you invoke it with .
you can find the name of the caller in $0

ASKER CERTIFIED SOLUTION
Avatar of woolmilkporc
woolmilkporc
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
That's total nonsense: $0 will of course only show setup.sh itself and not the calling script!
The viriable PPID will hold the parents process id.
Under
/proc/$PPID
you will find everything you want to know about the calling process!
OK,
 
 it seems that you want to find out the name of the calling ("parent") script!
 
 There is a variable containing the PID of the parent, which you could evaluate in the following way:

parent=$(ps -f -p $PPID | tail -1 | awk '{print $NF}')

... and again for the scriptname alone:

 parentfile=$(basename $(ps -f -p $PPID | tail -1 | awk '{print $NF}'))
Avatar of TerryNJones
TerryNJones

ASKER

Exactly what I needed!  Thanks.
It's not that easy, woolmilkporc ... That why I proposed to use all information on the calling process.
If the calling process had been call with some parameters, say
myscript -p1 -p2
then look what your "solution" does ...

@hemmi: $0 (better: ${0}) together with basename was the correct answer. Please do not other people the way you do and call solutions nonsense.
I'm sorry. I did not see the leading "." in the original post. So it seems guaranteed, that he's always sourcing the script and not calling the script!
Still - in case of actually calling - your basename will not work in all cases.