1

I have a library script named A and a script B, C which includes A with

. ../../../A 

The problem is how A can know which time I run ./B.sh or ./C.sh, example:

if(run ./B.sh)
   echo "B (file path) is calling"
else
   echo "C (file path) is calling"
0

1 Answer 1

2

You can use $0 to determine the command that was executed:

A.sh:

echo $0

B.sh:

. ./A.sh

When run:

$ sh B.sh 
B.sh
$ sh A.sh 
A.sh

It will only give the command that was executed, not the arguments:

$ sh B.sh one two three
B.sh
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks, it is the easiest way.
Be aware that using . ../../../A will only work relative to your current working directory. If you execute B.sh from another location the source built-in will fail. See stackoverflow.com/a/192305/341459 for a better way.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.