aquaop
aquaop

Reputation: 51

Get Current Script Name While Was Calling from Another Script

In KornShell (ksh), I have a script that calls a list of scripts. The callee script needs to know its file name, so that it can generate unique configuration (or anything should be unique).

The problem is variable $0 always points to the caller script. For example, we have two scripts:

caller.sh:

. callee.sh

callee.sh:

echo $0

When I execute caller.sh, the callee.sh print "caller.sh", not "callee.sh". So how can I get the script file name of current running script?

The script is running on AIX servers, so bash is not available all the time.

Upvotes: 3

Views: 6415

Answers (2)

Sawal Maskey
Sawal Maskey

Reputation: 648

Following command in callee.sh script worked for me.

echo $(basename ${BASH_SOURCE[0]})

Upvotes: 0

shellter
shellter

Reputation: 37268

It's because you are sourcing callee.sh, i.e. . callee.sh.

So if you can just execute callee.sh (and you don't need environment variables that are set in callee.sh to be active in caller.sh,) change your caller scritpt to

#!/bin/ksh
callee.sh

I hate to say it, but I think the ksh people would say 'Working as designed'.

Upvotes: 2

Related Questions