On Fri, May 17, 2024 at 03:32:23PM +0000, Matheus Afonso Martins Moreira wrote:
> > You don't have to export variables. I would recommend not exporting
> > such a variable to begin with unless you're sure of its effects.
> 
> It could be made safe so that it's possible to export it.
> Then it can always be set to some reasonable value.

"unset" is a very reasonable default value.  If BASH_SOURCE_PATH is
unset, then you get the historic bash behavior (or the POSIX behavior
if your shell is in POSIX mode).

Setting the variable at all is opting in to new behavior, and you do
that at your own risk, after reading the documentation and deciding
that this is what you want.

I do not foresee people setting BASH_SOURCE_PATH in their basic
interactive shell environments and login sessions.  Why would they
do that?  What purpose would it serve?

I could maybe see it being used in some sort of bash analogue of Python's
virtual environments.  Maybe you're building some bash project with
multiple resource files that get sourced from a subdirectory, and while
working in this project, you find it helpful to set BASH_SOURCE_PATH.
But you wouldn't want it to be set in your other windows.

What I'm imagining here is that the variable will be used almost
exclusively by scripts, to set the location of their resource files,
which they source.  These files may be "library code", or configuration
variables, or whatever.  They're not useful outside of the script which
sources them, so only that script needs to set BASH_SOURCE_PATH to find
them.

Effectively, it allows scripts to change from this:

    #!/bin/bash
    RSRCDIR=/opt/me/share
    source "$RSRCDIR/foo"
    source "$RSRCDIR/bar"

to this:

    #!/bin/bash
    BASH_SOURCE_PATH=/opt/me/share
    source foo
    source bar

Anything beyond that is up to you.

Reply via email to