jayunit100
jayunit100

Reputation: 17648

Should bash always provide a BASH_SOURCE definition, and if not, what is the best workaround?

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:

Upvotes: 2

Views: 184

Answers (1)

the.Legend
the.Legend

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

Related Questions