3

I would like to be able to use something like the BASH_ENV variable in zsh, however I can't find anything like it by Googling.

From man bash:

BASH_ENV

If this parameter is set when bash is executing a shell script, its value is interpreted as a filename containing commands to initialize the shell, as in ~/.bashrc. The value of BASH_ENV is subjected to parameter expansion, command substitution, and arithmetic expansion before being interpreted as a filename. PATH is not used to search for the resultant filename.

Does zsh have something like this?

1
  • 1
    @rowboat Note that BASH_ENV in bash and ENV in zsh is not the same thing. BASH_ENV is only ever used for non-interactive shells, while ENV in zsh is different, and only used in sh or ksh emulation mode. This is similar to how ENV in bash works when that shell is in POSIX compatibility mode. See man bash.
    – Kusalananda
    Jan 1, 2022 at 8:03

1 Answer 1

2

The closest I can think of is ZDOTDIR. Zsh reads its user initialization files in the directory $ZDOTDIR, falling back to $HOME if ZDOTDIR is unset. When zsh is running a script, it loads the user initialization file .zshenv.

So if in bash you'd have a file /path/to/my-bash-env containing

wibble

and you'd run env BASH_ENV=/path/to/my-bash-env /path/to/script.bash, then in zsh you can have a file /path/to/my-zsh-dir/.zshenv containing

if [[ -e ~/.zshenv ]]; then . ~/.zshenv; fi
wibble

and run env ZDOTDIR=/path/to/my-zsh-dir /path/to/script.zsh.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .