|
|
|
|
||||||
| comp.unix.shell Using and programming the Unix shell. |
![]() |
|
|
LinkBack | Outils de la discussion |
|
|
#1 |
|
Messages: n/a
Hébergeur: |
How to get the script name when sourcing it?
${0} returns '-bash'. Thanks a lot. |
|
|
|
#2 |
|
Messages: n/a
Hébergeur: |
$BASH_SOURCE troyzeng wrote: > How to get the script name when sourcing it? > ${0} returns '-bash'. > > Thanks a lot. |
|
|
|
#3 |
|
Messages: n/a
Hébergeur: |
On 25 Apr, 11:46, mop2 <mop2bky4mz5tyjwa8ersp7hrg5u...@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. |
|
|
|
#4 |
|
Messages: n/a
Hébergeur: |
2008-04-24, 19:57(-07), troyzeng:
> On 25 Apr, 11:46, mop2 <mop2bky4mz5tyjwa8ersp7hrg5u...@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 |
|
|
|
#5 |
|
Messages: n/a
Hébergeur: |
On 25 Apr, 16:42, Stephane CHAZELAS <this.addr...@is.invalid> wrote:
> 2008-04-24, 19:57(-07), troyzeng: > > > On 25 Apr, 11:46, mop2 <mop2bky4mz5tyjwa8ersp7hrg5u...@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 My question really should be: how to get the script name within the script when the script is meant to be sourced (to setup environment like a profile)? E.g., my script is called a.ksh. I want to find out the script name within a.ksh. If a.ksh is executed, ${0} is the name of the script (or the first part of the command line); when a.ksh is sourced, what is equivalent to $[0}? Troy |
|
|
|
#6 |
|
Messages: n/a
Hébergeur: |
2008-04-25, 02:05(-07), troyzeng:
[...] > My question really should be: how to get the script name within the > script when the script is meant to be sourced (to setup environment > like a profile)? > E.g., my script is called a.ksh. I want to find out the script name > within a.ksh. If a.ksh is executed, ${0} is the name of the script (or > the first part of the command line); when a.ksh is sourced, what is > equivalent to $[0}? [...] So I think you can't unless the caller has made it available in a variable (as in my source() function example) or unless you assume ksh is implemented with zsh. -- Stéphane |
|
![]() |
| Outils de la discussion | |
|
|