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.

4
  • Please take a look at this question: stackoverflow.com/questions/20572934/… IMHO this should be what you need. Commented Jul 14, 2020 at 10:19
  • Does this answer your question? Get the name of the caller script in bash script Commented Jul 14, 2020 at 10:39
  • @nikitaB : Why are you printing $BASH_SOURCE in fake_parent.sh and not in fake_child.sh? Commented Jul 14, 2020 at 10:42
  • 1
    @ahuemmer PARENT_COMMAND=$(ps -o comm= $PPID) is printing "bash" and PARENT_COMMAND=$(ps -o args= $PPID) is printing "-bash" Commented Jul 14, 2020 at 10:54

1 Answer 1

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" 
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.