10

I want to start using zsh as recommended by Apple for Mac OS Catalina. How do I migrate my aliases that I have defined in the ~/.bash_profile to the new shell?

nohillside
  • 100,768
displayName
  • 2,625
  • AFIK, the syntax for the alias definition works the same in bash and zsh, so unless you have some specific bashism in your aliases, you can simply copy and paste them. One point to be aware of, is that bash by default does not expand aliases in interactive shells, zsh does, and if you want to mimic this, you have to request this bash-like behaviour in zsh. However, if you place your aliases into .zshrc, the difference does not matter, since this file is processed by interactive shells only. – user1934428 Nov 15 '23 at 14:40

2 Answers2

17

If until now you've been using ~/.bash_profile for the loading the aliases, here are some of the ways you can migrate your aliases:


1. Copy the contents:

The most assiduous and obvious way is to copy the contents of your ~/.bash_profile to ~/.zshrc. It works particularly when you are moving on from bash to zsh for good.


2. Pull out aliases and then source them:

Another *way is:

  1. Create a new file that will contain the aliases (let's call it ~/.aliases);
  2. Copy the contents of ~/.bash_profile to the new aliases file;
  3. Modify the ~/.bash_profile and ~/.zshrc to source the new file.

Put the following in ~/.bash_profile:

if [ -f ~/.aliases ]; then
   . ~/.aliases
fi

and put the following in ~/.zshrc:

source $HOME/.aliases

This way the aliases will be available for both shells. More importantly, if you make changes to the aliases, the changes will cascade to both shells automatically.


* Taken from here

displayName
  • 2,625
1

I usually copy the whole file and in case delete the lines that I don't need. This always worked well for me:

cat .bash_profile >> .zshrc
G M
  • 307