Everytime I open the terminal, I have to source .bash_profile
to enable the $JAVA_HOME
or other variables.
11 Answers
Yes, it's called ~/.zshenv
.
Here's how I have $JAVA_HOME
set in ~/.zshenv
:
export JAVA_HOME="$(/usr/libexec/java_home)"
Keep in mind, however, that zsh is not bash, so just 'cause you have to source your .bash_profile
every time you open a terminal does not mean that you have to do that with zsh. With zsh, I only have to re-source my ~/.zshenv
when I make changes to it, and then only for terminals which are already open: new terminals should have already sourced my new and improved ~/.zshenv
.
NOTE
I often find it helpful, when trying to determine which of my zsh startup files I should place things in to consult zsh startup files.
A newer version of the documentation for startup files can be found here.

- 4,833
- 3
- 26
- 40
-
52Actually, `.zprofile` is closer in meaning to `.bash_profile`, in that both are only sourced by their respective shells for login shells. `.zshenv` is executed for *all* instances of `zsh`, whether or not they are login shells. – chepner Apr 16 '14 at 13:16
-
13@chepner `.zshenv` is where all environment variables should be defined. See [here](http://zsh.sourceforge.net/Intro/intro_3.html) – Alexej Magura Apr 16 '14 at 16:55
-
5Agreed; `.bash_profile` has to do for `bash` what `.zshenv` and `.zprofile/.zlogin` together do for `zsh`. That link provides good information on what should go where. – chepner Apr 16 '14 at 17:05
-
@Alexej Problem with defining `PATH` in `.zshenv` is that it could later be overridden, say by `/etc/profile`; see [PATH variable in .zshenv or .zshrc](https://stackoverflow.com/a/34244862/183120). – legends2k Nov 26 '20 at 13:09
-
@AlexejMagura I used to think that. `.zshenv` is sourced for *all* shells, though, including non-interactive shells. I would stick with `.zprofile` unless you have a *very* good reason for affecting other scripts. – chepner Jun 07 '21 at 12:11
-
@ennth Both will be sourced for interactive login shells; use `.zprofile` for things that should be inherited by non-`zsh` processes (like `PATH` or program-specific environment variables); use `.zshrc` for things specific to `zsh` itself, like `PS1` (which does *not* need to be exported). – chepner Jun 07 '21 at 12:12
-
@chepner According to the documentation for Zsh, `.zprofile`/`.zlogin` should not affect the shell environment at all. – Alexej Magura Aug 03 '21 at 18:59
-
1I disagree with that comment. `.zshenv` is sourced for *any* shell (including non-login interactive shells and non-interactive shells), which means it would be used far more often than is necessary for configuring your environment variables. I restrict that file to `zsh`-specific things, like `zstyle` commands: things that will not be *inherited* from a parent process. – chepner Aug 03 '21 at 19:17
-
@chepner All I'm saying is that the documentation (and by extension the developers) don't agree with putting environmental settings in `.zprofile`/`.zlogin`. – Alexej Magura Aug 04 '21 at 00:39
-
2It's not in the current man page, though. Note that your link was last updated in 1995. – chepner Aug 04 '21 at 11:45
-
@chepner Ah, fair enough. – Alexej Magura Aug 05 '21 at 18:33
I know this is an old question, but I recently upgraded MacOs to Catalina which changed the default shell from bash to zsh.
I ended up doing this:
echo source ~/.bash_profile > ~/.zshenv && source ~/.zshenv
To have zsh source my original .bash_profile.

- 4,129
- 2
- 27
- 25

- 3,819
- 3
- 27
- 48
-
17Use the command like this to ensure that the file is created in the users home directory: `echo source ~/.bash_profile > ~/.zshenv` – Smair May 06 '20 at 18:46
-
-
3@QASam, echo is a must here, `X > Y` only pass what X outputted. Without echo, nothing is outputted. – Alex Thai Aug 30 '20 at 19:14
-
This works to find my aliases but I still need to source to get the terminal to lad the correct prompt/colors for some reason. – akerr Mar 23 '21 at 18:41
-
Recently, with the upgrade to macOS Catalina, the default shell changed to zsh, which uses ~/.zshrc
as the resource file.
We usually had ~/.bash_profile
inside user home directory the solution is to simply
- Open
~/.bash_profile
by runningvim ~/.bash_profile
- Open
~/.zshrc
by runningvim ~/.zshrc
- Copy the content of
~/.bash_profile
into~/.zshrc
Open a new terminal window and run your previous aliases/scripts, which should work flawlessly.

- 1,421
- 13
- 28

- 4,315
- 1
- 28
- 19
-
5
-
note that all aliases should also be added at the bottom of `~/.zshrc`. – Dio Phung May 02 '23 at 01:49
Other simple alternative to continue using your .bash_profile is add this file to your .zshrc file:
- Open your .zhsrc file > vim ~/.zshrc
- Add this line to your .zshrc file > source ~/.bash_profile
with this simple solution you can continue adding your .bash_prifile if you like zhs.

- 123
- 1
- 6
-
could there be some differences between bash and zsh that caused bash_profile not to start properly? see http://zsh.sourceforge.net/FAQ/zshfaq02.html#l14 – framontb May 22 '20 at 16:29
There are five separate profile scripts that get executed (in the order given below) when we launch a zsh shell or close it out.
(1) .zshenv
--> This is always sourced first but can be overridden by other
(2).zprofile
--> This is equivalent for users coming from ksh experience
(3).zshrc
--> This is for all of the interactive customizations of zsh
(4).zlogin
--> This executes after first three are done
(5).zlogout
--> This is executed when we logout of the zsh shell
it would be advisable to put your stuff in .zshenv or in .zshrc
It is not mandatory to have any one of these files. But if it is there, it will be sourced from and executed in the above order.

- 111
- 1
- 4
In Mac Catalina onwards osx versions, the terminal uses zsh. There is a system-wide profile /etc/zprofile
.
Output example for cat /etc/zprofile
:
# System-wide profile for interactive zsh(1) login shells.
# Setup user specific overrides for this in ~/.zprofile. See zshbuiltins(1)
# and zshoptions(1) for more details.
if [ -x /usr/libexec/path_helper ]; then
eval `/usr/libexec/path_helper -s`
fi
it says , if you want to override then create ~/.zprofile
.
touch ~/.zprofile.

- 1,291
- 17
- 20

- 596
- 6
- 19
update: macOS Monterey 12.4
yes - for Zsh, it is the file: .zshrc
add there your parameter.

- 31
- 3
-
This does not provide an answer to the question. Once you have sufficient [reputation](https://stackoverflow.com/help/whats-reputation) you will be able to [comment on any post](https://stackoverflow.com/help/privileges/comment); instead, [provide answers that don't require clarification from the asker](https://meta.stackexchange.com/questions/214173/why-do-i-need-50-reputation-to-comment-what-can-i-do-instead). - [From Review](/review/late-answers/32206746) – cabad Jul 15 '22 at 20:27
In Mac Catalina, terminal uses zsh. Instead of having .bash_profile, good to have .zshenv and write your script there.
When you open terminal next every time, scripts inside .zshenv gets executed.

- 1,423
- 1
- 20
- 22
I was running into this issue and I followed Zack and Luke Schoen's answer, but my $PATH
didn't look the same as what I had in bash.
This post explains what the different config files do: https://unix.stackexchange.com/questions/71253/what-should-shouldnt-go-in-zshenv-zshrc-zlogin-zprofile-zlogout
I found that splitting my .bash_profile
path exports into .zprofile
and my aliases into .zshrc
worked best for what I wanted.
I found why Zack and Luke Schoen's answer didn't work for me:
The path exports that I listed in .zshenv
were executed first and /usr/libexec/path_helper
was executed afterwards,
which prepended the paths listed in /etc/paths
.