The right way to migrate your bash_profile to zsh
It's not only about zshrc
I’ve upgraded my Mac from macOS Mojave to macOS Catalina and the first thing I noticed is that zsh is the new default shell, leaving Bash behind. I decided to switch but I found a few inconveniences with the setup, mainly with porting over my existing
zsh startup files
bash_profile is bash specific, whereas
zsh has its own set of startup files. I’ve seen people generally accept the suggestion of moving everything from
.zshrc. It definitely works, but it’s not the strict appropriate way to do it.
zsh introduces 5 startup files, each one sourced depending on the type of shell invocation. The two most important are
.zshenv which, according to the man page and some other resources , works like this:
.zshenv: Sourced on all invocations of the shell. It often contains exported variables that should be available to other programs. For example,
.zshrc: Sourced in interactive shells only. It should contain commands to set up aliases, functions, options, key bindings, etc.
According to this, if you rely solely on
.zshrc, your non-interactive shell scripts won’t work if they depend on the
$PATHand any other important ENV variable should be set in
.zshenv. The rest can go into
Splitting instead of migrating
To put it in layman’s terms:
- Copy your
~/.bash_profileand copy them to
~/.zshenv(create the file if you don’t have it).
- Copy your alias, functions, key bindings and more from
- Restart your terminal
Custom alias and functions
I keep my alias and functions in a separate file so that it doesn’t clutter
zshrc. You can try to do the same:
- Copy your custom alias and functions from
- Paste these commands in a file called
- Put this line at the end of your
VSCode will not catch up
zsh as the default shell automatically. Adjust this with the following steps:
- Open VSCode settings. Mac users can do
CMD + Shift + Pand type “settings”.
- Open the JSON view (top right button)
Type this at the end of the file:
- Restart VSCode.