0

I am having two scripts: parent and child.

>>cat fake_parent.sh

#!/bin/sh
my_child(){
echo "I am parent "
./fake_child.sh
}

>>cat fake_child.sh

#!/bin/sh
echo "I am  child"
echo "I want to know my parent ...i.e fake_parent.sh here"

I am sourcing fake_parent.sh and calling my_child function; which should print "fake_parent.sh" at the end of the echo command. I have tried $BASH_SOURCE and $0 but both are giving me "fake_child.sh"

Please help.

nikitaB
  • 1
  • 2
  • Please take a look at this question: https://stackoverflow.com/questions/20572934/get-the-name-of-the-caller-script-in-bash-script IMHO this should be what you need. – ahuemmer Jul 14 '20 at 10:19
  • Does this answer your question? [Get the name of the caller script in bash script](https://stackoverflow.com/questions/20572934/get-the-name-of-the-caller-script-in-bash-script) – Cristian Ramon-Cortes Jul 14 '20 at 10:39
  • @nikitaB : Why are you printing `$BASH_SOURCE ` in `fake_parent.sh` and not in `fake_child.sh`? – user1934428 Jul 14 '20 at 10:42
  • 1
    @ahuemmer PARENT_COMMAND=$(ps -o comm= $PPID) is printing "bash" and PARENT_COMMAND=$(ps -o args= $PPID) is printing "-bash" – nikitaB Jul 14 '20 at 10:54

1 Answers1

0

You can try this:

# fake_parent.sh
my_child(){
echo "I am parent "
export PARENT="$(basename $0)"
./fake_child.sh
}

# fake_child.sh
#!/bin/sh
echo "I am  child"
echo "I want to know my parent ... $PARENT"
UtLox
  • 3,744
  • 2
  • 10
  • 13