From: troyzeng on
How to get the script name when sourcing it?
${0} returns '-bash'.

Thanks a lot.
From: mop2 on

$BASH_SOURCE

troyzeng wrote:
> How to get the script name when sourcing it?
> ${0} returns '-bash'.
>
> Thanks a lot.
From: troyzeng on
On 25 Apr, 11:46, mop2 <mop2bky4mz5tyjwa8ersp7hrg5u...(a)gmail.com>
wrote:
> $BASH_SOURCE
>
> troyzeng wrote:
> > How to get the script name when sourcing it?
> > ${0} returns '-bash'.
>
> > Thanks a lot.

Thanks. That works for bash and sh.
What about ksh? Thanks.
From: Stephane CHAZELAS on
2008-04-24, 19:57(-07), troyzeng:
> On 25 Apr, 11:46, mop2 <mop2bky4mz5tyjwa8ersp7hrg5u...(a)gmail.com>
> wrote:
>> $BASH_SOURCE
>>
>> troyzeng wrote:
>> > How to get the script name when sourcing it?
>> > ${0} returns '-bash'.
>>
>> > Thanks a lot.
>
> Thanks. That works for bash and sh.

Only for sh where sh is implemented by bash, so only GNU
systems.

> What about ksh? Thanks.

Depends on the implementation of ksh. In the zsh implementation,
you'll find it in ${(%):-%N} (%N, in prompt expansion is
expanded to the name of the currently sourced script (for PS2),
(%) enables prompt expansion in parameter expansion ${:-default}
is a special form of ${var:-default} which always expands to
"default").

Portably, you could do:

source() {
sourced_script=$1
. "$1"
}

And use "source" instead of ".". And then use $sourced_script.

--
St�phane