I know that in bash you can set up aliases in a .bash_aliases file, so that the command you type doesn't need to be a command stored in the binaries in the system. Is there any way I can get aliases into zsh?
- 16,936
- 80,112
7 Answers
I go back and forth between bash and zsh, and use the same .aliases file for both. They share the same basic alias syntax, so you can create a .aliases file and link it to .bashrc and .zshrc:
.bashrc
if [ -f ~/.aliases ]; then
. ~/.aliases
fi
.zshrc
source $HOME/.aliases
FWIW this can also be done with environment variable declarations, in a separate .env file.
You can do it by the "alias" command with this syntax:
alias [ -gmrL ] [ name[=value] ... ]
For "gmrL" switches, see this guide, which is my reference.
For each name, with no value, zsh will print the name and what it is aliased to previously. With no arguments at all, alias prints the values of ALL defined aliases.
To define one or more aliases, simply enter:
alias name1=value1 name2=value2 ... nameX=valueX
For each name with a corresponding value, zsh defines an alias with that value. For further info, check out that link. ;-)
-
-
12this answer misses the part about where to persist the aliases, Kurtosis answer includes it (.zshrc). – Felix Jul 31 '14 at 09:06
-
6I use
~/.profileto store all aliases I care about and source~/.profileit from~/.zshrc. – danba Nov 12 '18 at 20:00 -
I tried to store all my aliases in
~/.zprofilebut most are overwritten @danba – alper Jul 07 '20 at 22:04
You generally put them in ~/.zshenv. But many programs use /bin/sh (usually bash) instead of $SHELL to execute shell commands, so for it to work everywhere you will probably need to put the bash equivalent of the alias into ~/.bash_aliases anyway.
- 11,787
-
This is for my user account only, so this does not need to be copied to the ~/.bash_aliases file. – Thomas Ward Mar 20 '11 at 18:02
-
2According to this, aliases should go in
.zshrcbecause.zshenvis "Read every time" -- "this file is read even when Zsh is launched to run a single command." – AWhitford Oct 16 '20 at 22:17 -
As
.zshenvis always sourced, the aliases would even affect scripts, so this is a bad idea. – paradroid Apr 27 '23 at 17:19
I was trying some things and I found a way to use my aliases created in bash into zsh, only I had to copy these lines from bashrc:
if [ -e ~/.bash_aliases ]; then
. ~/.bash_aliases
fi
- 387
- 4
- 12
- 41
- 2
What I've found from official guide for Zsh it could be done by code below in your .zshrc:
if [[ -r ~/.aliasrc ]]; then . ~/.aliasrc fiwhich checks if there is a readable file
~/.aliasrc, and if there is, it runs it in exactly the same way the normal startup files are run. You can usesourceinstead of.if it means more to you;.is the traditional Bourne and Korn shell name, however.
- 16,936
- 21
-
OK so
.aliasrcin ZSH is the equivalent of.bash_aliasesin Bash. This should be the accepted answer. – WesternGun Jun 18 '25 at 11:29
.zshrc
add this line at the bottom of the file (assuming that your aliases located in ~/.profile):
source ~/.profile
-
Don't do this. Depending on what's in
~/.profile, you might end up with weird errors or, worst case, an unusable shell, since~/.profilein turn sometimes calls another shell's rc file (e.g. you could end up with a circular reference). Also~/.profileis meant to be executed once per login, not once per terminal process. I would source~/.bash_aliasesor~/.aliases(whichever exists) instead. – Dale C. Anderson Mar 23 '23 at 21:32
If anyone find this useful: My situation is that I have a Macbook Laptop, Ubuntu Laptop, Ubuntu Desktop and couple of Ubuntu VMs. In all of them I want to use defaults (so Bash in Ubuntu and Zsh in OSX) but with same aliases.
The way I handle it is that I have my alias file .bash_aliases in git repo called dotfiles. I just clone the repo in all my computers and I just create a symlink to the alias file:
ln -s "~/wherever_i_store_git_repos/dotfiles/.bash_aliases" "~/.bash_aliases"
in Ubuntu I add this to ~/.bashrc :
if [ -f ~/.bash_aliases ]; then
. ~/.bash_aliases
fi
in OSX I add this to .zshrc :
if [ -f ~/.bash_aliases ]; then
source $HOME/.bash_aliases
fi
note: Alternatively if you don't use git you can ln -s a Dropbox folder
- 561
- 2
- 5
- 12
like this:
– lewis4u Apr 13 '17 at 11:26~/ .bash_aliasesI need little help please if you are there, respond[ -r "$HOME/.aliases" ] && source "$HOME/.aliases"? (nice doc) – Pablo Bianchi Apr 07 '20 at 16:09
– appel Feb 28 '25 at 15:44[ -f ~/.zshrc ] && echo 'source ~/.bash_aliases' >> ~/.zshrc