Thanks for clarifying! I wondered if I was missing something, but kept seeing
`${BASH_SOURCE[0]/%/*}` suggested in the thread, which I pointed out is flawed.
I guess what I want is a "BASH_SOURCE_DIR" variable or something like it,
mainly to avoid the boilerplate, variable, and/or command substitution in each
of my main scripts.
Note that ShellCheck uses a similar SCRIPTDIR to help locate sourced files
relative to the main script:
https://github.com/koalaman/shellcheck/issues/1577
-Will
On Wednesday, June 19, 2024 at 03:13:59 PM PDT, konsolebox
<[email protected]> wrote:
On Thu, Jun 20, 2024 at 4:05 AM Will Allan <[email protected]> wrote:
> But, I still don’t like it. I have to start off each script with a slow
> command substitution (subshell) which introduces a variable that I don’t
> really want, but it’s too slow to do this repeatedly:
I agree.
> source -- "${BASH_SOURCE_PATH}/../lib/foo.sh"
You misunderstood the use of BASH_SOURCE_PATH though. It's proposed
to be an alternative to PATH which is only respected by source. I
suggest another name. And that is BASH_SOURCE_REAL.
Alternatively, have BASH_SOURCE always produce real physical paths
either by default or through a shopt.
Any additional feature that doesn't allow loading a script relative to
the caller without external help is practically useless.
--
konsolebox