Reputation: 17648
I recently found that some scripts referencing BASH_SOURCE were failing because it is undefined in my zshell. Note: My bash is coming from homebrew, so possibly homebrew's bash changes default BASH_SOURCE.
I have two questions regarding this matter:
Question 1: Is $BASH_SOURCE
always defined as supported in any bash shell? If so, I assume running /bin/bash
and not seeing BASH_SOURCE implies a bug in bash itself?
Question 2 : Is there a reasonable default I should globally export for BASH_SOURCE which wont break anything?
Upvotes: 2
Views: 184
Reputation: 686
BASH_SOURCE is array which contains source filenames where the corresponding shell function names in the FUNCNAME are defined. FUNCNAME containins the names of all shell functions currently in the execution call stack. This means BASH_SOURCE is specific to current session.
In general, you're referring to variable specific to BASH, other shells have different implementation of call stack handling and thus will not handle it properly
When running script the first line #!/bin/bash
specifies interpreter for current script. If you use bash, you should provide bath to BASH installation there
Alternatively switch to bash and run the script there. Or run it by passing script as an argument to /bin/bash from zsh. Whichever you prefer
Upvotes: 1