> Not sure how common but this is what makes sense. Or name sourceables
> foo.sh, bar.sh and executables foo, bar so they don't clash and source with
> `${BASH_SOURCE%/*}' prepended to PATH and it'll work fine.
> 
> This feature request sounded promising at first, it feels like
> bike-shedding now.

I think this is exactly why this feature is necessary. Unless I am 
misunderstanding, simply prepending `${BASH_SOURCE%/*}' to a sourced path will 
not work in all cases. For example, when executing the main script directly 
inside the script directory (e.g. `bash main.sh`) you would end up with an 
invalid path (e.g. main.sh/../lib/foo.sh).

To solve such a seemingly simple problem when first introduced to Bash, I 
turned to StackOverflow. Note this question has hundreds of answers of varying 
complexity with thousands of up and downvotes. Yikes!

https://stackoverflow.com/questions/59895/how-do-i-get-the-directory-where-a-bash-script-is-located-from-within-the-script

Since I find the accepted answer to be overly complex for my needs, I usually 
just do this:

declare -r SCRIPT_DIR="$(dirname -- "${BASH_SOURCE[0]}")"
source -- "${SCRIPT_DIR}/../lib/foo.sh"
source -- "${SCRIPT_DIR}/../lib/bar.sh"
...

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:

source -- "$(dirname -- "${BASH_SOURCE[0]}")/../lib/foo.sh"
source -- "$(dirname -- "${BASH_SOURCE[0]}")/../lib/foo.sh"
...

I would love to be able to do this for efficiency and readability (see below). 
This would prevent future users from having to sift through hundreds of faulty 
StackOverflow answers like I did.

source -- "${BASH_SOURCE_PATH}/../lib/foo.sh"
source -- "${BASH_SOURCE_PATH}/../lib/bar.sh"
...

-Will


Reply via email to