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"
pfnuesel
  • 14,093
  • 14
  • 58
  • 71
Pham Huu Bang
  • 105
  • 2
  • 10

1 Answers1

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
Lee Netherton
  • 21,347
  • 12
  • 68
  • 102
  • Thanks, it is the easiest way. – Pham Huu Bang Jan 28 '16 at 13:16
  • 1
    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 http://stackoverflow.com/a/192305/341459 for a better way. – Lee Netherton Jan 28 '16 at 13:21