[Novalug] How can script know if it's been 'sourced' rather than
'called'?
Ben Creitz
creitz at gmail.com
Fri Apr 6 15:24:30 EDT 2007
On 4/6/07, Michael Stone <mstone at mathom.us> wrote:
> On Fri, Apr 06, 2007 at 02:52:38PM -0400, Richard Rognlie wrote:
> >$ ./test.sh
> >SHELL=/bin/bash
> >0=./test.sh
> >called
> >
> >$ . ./test.sh
> >SHELL=/bin/bash
> >0=/bin/bash
> >sourced
> >
> >$ bash test.sh
> >SHELL=/bin/bash
> >0=test.sh
> >called
>
> user's shell is zsh:
> % . ./test.sh
> SHELL=/usr/bin/zsh
> 0=./test.sh
> called
>
> user's shell is sh:
> $ . ./test.sh
> SHELL=/bin/sh
> 0=sh
> called
>
> user's shell is tcsh:
> > . test.sh
> SHELL=/usr/bin/tcsh
> 0=tcsh
> if: Expression Syntax.
OK, I see what you mean about assumptions. When we source a file, we
don't get the advantage of the #!/bin/myshell, and that's where the
problem begins, right?
Luckily, I can make all of the required assumptions because this
script is only for me, to be run from bash, so that I quickly kick
off a session during which I know I'll be logging into a bunch of
servers or running some script that wants to log in to a bunch of
servers. Having the thing know whether it had been sourced or not was
really just to satisfy my curiosity.
Thanks for the insights, all!
More information about the Novalug
mailing list