delorie.com/archives/browse.cgi   search  
Mail Archives: cygwin/1999/02/23/05:05:27

Mailing-List: contact cygwin-help AT sourceware DOT cygnus DOT com; run by ezmlm
Sender: cygwin-owner AT sourceware DOT cygnus DOT com
Delivered-To: mailing list cygwin AT sourceware DOT cygnus DOT com
Message-ID: <8135911A809AD211AF6300A02480D17503491A@iis000.microdata.fr>
From: Bernard Dautrevaux <DAUTREVAUX AT microprocess DOT com>
To: "'Christopher Warth'" <csw AT epigram DOT com>, gnu-win32 AT cygnus DOT com
Subject: RE: nesting "source" scripts
Date: Mon, 22 Feb 1999 14:36:09 +0100
MIME-Version: 1.0
X-Mailer: Internet Mail Service (5.5.1960.3)
X-MIME-Autoconverted: from quoted-printable to 8bit by delorie.com id FAA15532

> -----Original Message-----
> From: Christopher Warth [mailto:csw AT epigram DOT com]
> Sent: Sunday, February 21, 1999 11:59 PM
> To: gnu-win32 AT cygnus DOT 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 AT microprocess DOT com
		b DOT dautrevaux AT usa DOT net
-------------------------------------------- 

--
Want to unsubscribe from this list?
Send a message to cygwin-unsubscribe AT sourceware DOT cygnus DOT com

- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019