Finding a Bash script's Runfiles as generated by Bazel
The following snippet provides a cross-platform implementation


That looks correct, but the script requires Bash 4, and I don't see any validation of that.

Actually, the only Bash 4 feature is the -v VAR operator of [[,
used as [[ ! -v VAR ]].
I would replace that with [[ ! "$VAR" ]],
even if it's not exactly the same thing,
but probably good enough.



Do you really need the [[ ! -v EXEC_NAME ]] check?
I don't see a point in a caller overriding this,
so I would drop the condition.

From the snippet it's not clear why this variable is exported with the export keyword.
I would review this point, and if there is no reason to export it,
then drop the export.



It seems to me the script is intended to be executed,
as opposed to being sourced.
In this case, you could use $0 as the name of the executable,
no need for the tedious ${BASH_SOURCE[0]}.



According to help test:


-a FILE        True if file exists.
...
-e FILE        True if file exists.



Although using -a is correct, I prefer -e, because it's easy to associate with the word "exists".
I had to look up help test to verify what is -a.



Instead of printf "...\n" it's simpler to write echo "...",
the \n is automatically added,
and the posted snippet doesn't use any interesting feature of printf.



Instead of:


EXEC_PATH="${BASH_SOURCE[0]}"
...
export EXEC_PATH



I would write:

export EXEC_PATH="${BASH_SOURCE[0]}"