1

I've read that .bashrc (like .zshrc) is meant only for interactive logins (and the one non-interactive exception of remote shells). But where should environment variables for Bash be placed that is (roughly) equivalent to .zshenv?

iconoclast
  • 9,198
  • 13
  • 57
  • 97

1 Answers1

1

That would be the $BASH_ENV environment variable.

info bash BASH_ENV:

BASH_ENV
If this variable is set when Bash is invoked to execute a shell script, its value is expanded and used as the name of a startup file to read before executing the script. *Note Bash Startup Files::.

So you'd set that variable to ~/.bashenv for instance in your ~/.profile for all non-interactive bash instances though not the ones invoked as sh to interpret code in that file upon startup.

To do that for interactive ones as well, you can add a source ~/.bashenv to your ~/.bashrc (maybe also in your ~/.bash_profile if it doesn't already source your ~/.bashrc when interactive).

  • If you set $BASH_ENV in your ~/.profile, then isn't ~/.profile the one closest to /.zshenv for Bash? – iconoclast Mar 18 '22 at 15:44
  • 2
    ~/.profile is like ~/.zprofile, it's the login session initialisation file. You can set env vars that will be inherited by commands started within that session there. – Stéphane Chazelas Mar 18 '22 at 15:49