Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Finding absolute path in a sourced script

50 views
Skip to first unread message

Kenji Neoh

unread,
Sep 17, 2008, 1:13:57 AM9/17/08
to
Hello,

Sorry, that might be a FAQ... couldn't find anything on the web.

Let's say I have a script that is used to set environment variables,
people use it like this:

$ source /path/to/foo.bash

Is there a way in foo.bash to know where the foo.bash script is located
(absolute path) ?

Thanks,
Ken

wyh...@gmail.com

unread,
Sep 17, 2008, 2:09:59 AM9/17/08
to

pwd doesn't work?

Kenji Neoh

unread,
Sep 17, 2008, 2:39:16 AM9/17/08
to
On 2008-09-17, wyh...@gmail.com <wyh...@gmail.com> wrote:

Nope, it would return the current working dir. I want the path where the
sourced script is.

In the example above, if pwd was used in the foo.bash script, it would
return the current working dir, while what I want is "/path/to/".

thanks,
Ken

Grant

unread,
Sep 17, 2008, 2:48:47 AM9/17/08
to
On 17 Sep 2008 06:39:16 GMT, Kenji Neoh <ke...@laposte.net> wrote:

>On 2008-09-17, wyh...@gmail.com <wyh...@gmail.com> wrote:
>> On 9??17??, ????1??13??, Kenji Neoh <ke...@laposte.net> wrote:
>>> Hello,
>>>
>>> Sorry, that might be a FAQ... couldn't find anything on the web.
>>>
>>> Let's say I have a script that is used to set environment variables,
>>> people use it like this:
>>>
>>> $ source /path/to/foo.bash
>>>
>>> Is there a way in foo.bash to know where the foo.bash script is located
>>> (absolute path) ?
>>>
>>> Thanks,
>>> Ken
>>
>> pwd doesn't work?
>
>Nope, it would return the current working dir. I want the path where the
>sourced script is.

How can you source a script that you don't know where it is?

Puzzled.

Grant.
--
http://bugsplatter.id.au/

Chris F.A. Johnson

unread,
Sep 17, 2008, 3:16:41 AM9/17/08
to

You know where it is, so put it in the script.

If you need to know, you need to fix your methodology.

--
Chris F.A. Johnson, author <http://cfaj.freeshell.org/shell/>
Shell Scripting Recipes: A Problem-Solution Approach (2005, Apress)
===== My code in this post, if any, assumes the POSIX locale
===== and is released under the GNU General Public Licence

Kenji Neoh

unread,
Sep 17, 2008, 4:03:38 AM9/17/08
to
On 2008-09-17, Chris F.A. Johnson <cfajo...@gmail.com> wrote:
> On 2008-09-17, Kenji Neoh wrote:
>> Hello,
>>
>> Sorry, that might be a FAQ... couldn't find anything on the web.
>>
>> Let's say I have a script that is used to set environment variables,
>> people use it like this:
>>
>> $ source /path/to/foo.bash
>>
>> Is there a way in foo.bash to know where the foo.bash script is located
>> (absolute path) ?
>
> You know where it is, so put it in the script.
>
> If you need to know, you need to fix your methodology.
>

Well, I don't necessarily know where it is. I am writing foo.bash, but
someone else might be using/sourcing it.

Let's say I have a small package like this:

/foo.bash
/bin/foo
/lib/foo.so

Someone grabs that package, put it anywhere on his filesystem. I'd like
to know if it's possible that foo.bash can set the PATH and shared
object (LD_LIBRARY_PATH or whatnot) to point to the relative bin/ and
lib/ folder, whatever the install location is or whatever the workding
dir is when foo.bash is sourced.

That way the user can just source foo.bash and he is good to go.

Just wondering if it's possible.

Thanks,
Ken

amateur

unread,
Sep 17, 2008, 8:35:59 AM9/17/08
to
Hi,

If the requirement is to get the exact path and filename that's being
sourced, the following worked for me:

In the file being sourced use
echo ${PWD}/${BASH_ARGV}

This would show the absolute path of the file being sourced. If
relative path is required remove ${PWD}/

This worked for me on GNU bash, version 3.1.17(1)-release

If it doesn't work for you, just enter 'set' in the file and it will
show all the environment variables set when the file is being
sourced. And you should be able to figure out what would work for
you.

Thanks,
-Neeraj.

Kenji Neoh

unread,
Sep 17, 2008, 10:27:34 AM9/17/08
to

Thanks Neeraj, that's exactly what I want. I verified the man page, it
says that BASH_ARGV is only updated if extdebug mode is on (shopt -e
extdebug), but it seems to be working even if it's off on my system.

Is that a bug in bash or a bug in the man page or is there something I
am missing here ?

Thanks,
Ken

Maxwell Lol

unread,
Sep 17, 2008, 12:51:56 PM9/17/08
to
Kenji Neoh <ke...@laposte.net> writes:

>>> $ source /path/to/foo.bash
>>>
>>> Is there a way in foo.bash to know where the foo.bash script is located
>>> (absolute path) ?
>>
>> You know where it is, so put it in the script.
>>
>> If you need to know, you need to fix your methodology.
>>
>
> Well, I don't necessarily know where it is. I am writing foo.bash, but
> someone else might be using/sourcing it.
>

Give the customer an alias that sets a variable and sources the file.

> Let's say I have a small package like this:
>
> /foo.bash
> /bin/foo
> /lib/foo.so
>
> Someone grabs that package, put it anywhere on his filesystem. I'd like
> to know if it's possible that foo.bash can set the PATH and shared
> object (LD_LIBRARY_PATH or whatnot) to point to the relative bin/ and
> lib/ folder, whatever the install location is or whatever the workding
> dir is when foo.bash is sourced.

The location of the file should be in an environment variable.

Assuming csh

setenv TOP /path/to/foo
source $TOP/foo

and foo can add $TOP/bin to the searchpath.
An alias can do this....

Kenji Neoh

unread,
Sep 17, 2008, 9:39:41 PM9/17/08
to
On 2008-09-17, Maxwell Lol <nos...@com.invalid> wrote:
> Kenji Neoh <ke...@laposte.net> writes:

...

>
> The location of the file should be in an environment variable.
>
> Assuming csh
>
> setenv TOP /path/to/foo
> source $TOP/foo
>
> and foo can add $TOP/bin to the searchpath.
> An alias can do this....

Thanks that's a good idea.

Ken

0 new messages