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.
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
>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/
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
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
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.
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
>>> $ 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....
>
> 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