help-bash
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Help-bash] Interpreting BASH_SOURCE after changing working folder


From: João Eiras
Subject: [Help-bash] Interpreting BASH_SOURCE after changing working folder
Date: Tue, 24 Apr 2018 17:09:51 +0200

So, consider this simple example:

$ cat example_script.
echo PWD:$PWD
echo BASH_SOURCE:address@hidden
cd /tmp
echo PWD:$PWD
echo BASH_SOURCE:address@hidden
$ bash example_script.sh
PWD:/home/.../
BASH_SOURCE:example_script.sh
PWD:/tmp
BASH_SOURCE:example_script.sh

This shows that BASH_SOURCE includes the exact path used to invoke a
script, whether it was absolute or relative. And in case there was a
change in working folder, the values in BASH_SOURCE no longer match to
a file that can be found in the current folder.

In my use case, I'm coding libraries for testing, logging and
utilities which are quite agnostic to how the program in which they
are sourced is invoked, so any access to BASH_SOURCE needs to be
localized, without artifacts elsewhere to store the original PWD, for
instance.

The question is: how can I get the original PWD when the script was
invoked to help interpret BASH_SOURCE ?

Thank you !



reply via email to

[Prev in Thread] Current Thread [Next in Thread]