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?
Asked
Active
Viewed 1,225 times
0
iconoclast
- 9,057
- 12
- 56
- 95
1 Answers
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).
Stéphane Chazelas
- 522,931
- 91
- 1,010
- 1,501
-
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
-
1`~/.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