On Sat, 2024-Feb-17, Zachary Santer wrote:
On Sat, Feb 17, 2024, Marc Aurèle La France wrote:
Do ...
rm -fr GREPME grepme echo '#! /bin/bash\nset' > GREPME "ln" GREPME grepme chmod a+x grepme
... then (case matters) ...
BASH_ENV=GREPME ./grepme | fgrep -iw grepme
... gives ...
BASH_ARGV=([0]="GREPME") BASH_ENV=GREPME BASH_SOURCE=([0]="GREPME") _=./grepme BASH_ENV=GREPME BASH_SOURCE=([0]="./grepme")
... so $_ wins, not $BASH_SOURCE[0]
Sorry I didn't read your original email chain from 2021 before responding earlier.
You want a script sourced because it's given as ${BASH_ENV} to be able to tell what script it was sourced from?
If you're satisfied with $_ or now $0, then fine, but I would actually expect that to show up as ${BASH_SOURCE[1]} within the ${BASH_ENV} script, which it obviously doesn't. Don't know what ${BASH_LINENO[0]} ought to be in that case. Maybe 0?
The way the manual explains BASH_SOURCE and BASH_LINENO is all in reference to FUNCNAME, which doesn't exist (or is empty) if you're not executing a function. Still, with explicit sourcing, this is the behavior we see:
zsant@Zack2021HPPavilion MINGW64 ~/random $ cat original-file #!/bin/bash source ./sourced-file
zsant@Zack2021HPPavilion MINGW64 ~/random $ cat sourced-file declare -p BASH_SOURCE BASH_LINENO
zsant@Zack2021HPPavilion MINGW64 ~/random $ ./original-file declare -a BASH_SOURCE=([0]="./sourced-file" [1]="./original-file") declare -a BASH_LINENO=([0]="2" [1]="0")
So this would seemingly be more consistent and not require a whole new shell variable.
Well, I may find what you describe useful at some point, but for now $0 will do.
Thanks. Marc.