This is the mail archive of the cygwin@sourceware.cygnus.com mailing list for the Cygwin project. See the Cygwin home page for more information.
[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index] [Subject Index] [Author Index] [Thread Index]

RE: nesting "source" scripts



> -----Original Message-----
> From: Christopher Warth [mailto:csw@epigram.com]
> Sent: Sunday, February 21, 1999 11:59 PM
> To: gnu-win32@cygnus.com
> Subject: nesting "source" scripts
> 
> 
> 
> This looks like it is simply a bash bug rather than anything specific
> to cygwin but snce I encountered this using cygwin I will try to find
> the answer here first.
> 
> It looks like the full path name of bash scripts is not available when
> they are invoked with "source".  This makes it impossible to call
> other scripts in the same directory, no?

I think this is a bash feature :-)

> 
> Consider the two bash scripts in the same directory, A.sh and B.sh
> 
> A.sh --
> 	#!bash
> 	echo A: $0
> 	source B.sh
> B.sh -- 
> 	#!bash
> 	echo B: $0
> 	echo Inside of B!
> 
> If CWD (current working directory) is the same as the location of 
> A.sh and B.sh, you can simply type
> 
> 	$ source A.sh
> 	A: bash
> 	B: bash
> 	Inside of B!
> 
> and it will all work.
> 
> However, if CWD is someplace else, and you type
> 
> 	a/long/path/A.sh
> 
> it seems there is no way for A.sh to correctly find the B.sh script
> without hardcoding the path into A.sh.
> 
> You might think you would simply take the directory component of $0
> and use that to help find the full path of A.sh.  That does not work
> because when invoking a script with "source" (and only then!) $0 is
> always set to just "bash", not the path of script.
> 
> Thus as far as I can tell there is no way to nest scripts that might
> be invoked with "source", short of hardcoding paths.  Is this true?
> If so, it sure seems like a bug.
> 


No; it's not a bug. When you source a sub-script (either by source or
'.'), you do not start a new shell; you just redirect the input of the
current shell. 

This redirect does not change _any_ of the variables or positional
parameters of the shell: $0 is left unchanged, as $1 and the rest... In
exchange you can set up variables (or define functions) that will be
available from the sourcing script and you have access to all its
variables and functions.

If you want to create sourcable libraries you can load anywhere and
source from everywhere you _need_ some sort of conventions; for example
you may set up a variable (named e.g. scripts_sourcable_library_dir)
then do

scripts_sourcable_library_dir=/some/where/on/my/disk
source $scripts_sourcable_library_dir/A.sh

and in A.sh:

source $scripts_sourcable_library_dir/B.sh

OTOH if you do not need to pass back variables or functions to the
sourcing shell, just call the script and all will work like you expect.

FYI the "bash" in $0 just come from your interactive bash...

Hope this helps,

		Bernard

--------------------------------------------
Bernard Dautrevaux
Microprocess Ingéniérie
97 bis, rue de Colombes
92400 COURBEVOIE
FRANCE
Tel:	+33 (0) 1 47 68 80 80
Fax:	+33 (0) 1 47 88 97 85
e-mail:	dautrevaux@microprocess.com
		b.dautrevaux@usa.net
-------------------------------------------- 

--
Want to unsubscribe from this list?
Send a message to cygwin-unsubscribe@sourceware.cygnus.com