130

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?

8 Answers 8

211

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.

5
  • 3
    I just added source ~/ .bash_aliases to the end of .zshrc, so all aliases are working both on bash and zsh
    – talsibony
    Feb 3, 2017 at 9:04
  • @talsibony like this: ~/ .bash_aliases I need little help please if you are there, respond
    – lewis4u
    Apr 13, 2017 at 11:26
  • @lewis4u There should not be a space between ~/ and .bash_aliases. It should be exactly ~/.bash_aliases.
    – bgibson
    Jun 16, 2017 at 20:54
  • 2
    Your links are broken..
    – Ilan.b
    Aug 19, 2019 at 8:48
  • Both links are broken. Wouldn't both the same as [ -r "$HOME/.aliases" ] && source "$HOME/.aliases"? (nice doc) Apr 7, 2020 at 16:09
57

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. ;-)

6
  • That document answered my question exactly. Thanks much.
    – Thomas Ward
    Mar 21, 2011 at 3:22
  • That link is down. Apr 14, 2014 at 10:52
  • updated the link.
    – sazary
    Apr 14, 2014 at 17:54
  • 12
    this answer misses the part about where to persist the aliases, Kurtosis answer includes it (.zshrc).
    – Felix
    Jul 31, 2014 at 9:06
  • 6
    I use ~/.profile to store all aliases I care about and source ~/.profile it from ~/.zshrc.
    – danba
    Nov 12, 2018 at 20:00
12

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.

3
  • This is for my user account only, so this does not need to be copied to the ~/.bash_aliases file.
    – Thomas Ward
    Mar 20, 2011 at 18:02
  • 2
    According to this, aliases should go in .zshrc because .zshenv is "Read every time" -- "this file is read even when Zsh is launched to run a single command."
    – AWhitford
    Oct 16, 2020 at 22:17
  • As .zshenv is always sourced, the aliases would even affect scripts, so this is a bad idea.
    – paradroid
    Apr 27, 2023 at 17:19
4

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
0

.zshrc

add this line at the bottom of the file (assuming that your aliases located in ~/.profile):

source ~/.profile
1
  • Don't do this. Depending on what's in ~/.profile, you might end up with weird errors or, worst case, an unusable shell, since ~/.profile in turn sometimes calls another shell's rc file (e.g. you could end up with a circular reference). Also ~/.profile is meant to be executed once per login, not once per terminal process. I would source ~/.bash_aliases or ~/.aliases (whichever exists) instead. Mar 23, 2023 at 21:32
0

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

0

What I've found from official guide for Zsh it could be done by code below in your .zshrc:

if [[ -r ~/.aliasrc ]]; then
    . ~/.aliasrc
fi

which 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 use source instead of . if it means more to you; . is the traditional Bourne and Korn shell name, however.

-2

I wrote this and put it in my ~/.bashrc a long time ago. I didn't put all that can be done with saving your settings, but just use your imagination!

Note: If you're updating ~/.bash_aliases then update ~/.oh-my-zsh/.zsh_aliases too.

Saves your ~/.bash_history, ~/.bashrc, ~/.bash_aliases and ~/.profile in Dropbox. First make a file named after your files in the Dropbox folder. That way no matter where you edit it, if it's edited? It's up-to-date on all you devices. Add the following lines in your ~/.bashrc :

    DBPATH=${HOME}"/Dropbox"; ## Path to your DropBox Folder
if [ -d "${DBPATH}" ] 
then
 
    BA="/.bash_aliases" ## Path to your .bash_aliases File
if [ ${HOME}"${BA}" -nt ${DBPATH}"${BA}" ] 
then
    cat ${HOME}"${BA}" > ${DBPATH}"${BA}" 2>/dev/null;
# If updating ~/.bash_aliases then update ~/.oh-my-zsh/.zsh_aliases Too
    cat ${HOME}"${BA}" > ${ZSH}"/.zsh_aliases" 2>/dev/null;
 fi

    BRC="/.bashrc"  ## Path to your .bashrc File
if [ ${HOME}"${BRC}" -nt ${DBPATH}"${BRC}" ] 
then
    cat ${HOME}"${BRC}" > ${DBPATH}"${BRC}" 2>/dev/null;
 fi
 
    BPF="/.profile" ## Path to your bash .profile File
if [ ${HOME}"${BPF}" -nt ${DBPATH}"${BPF}" ] 
then
    cat ${HOME}"${BPF}" > ${DBPATH}"${BPF}" 2>/dev/null;
 fi

    BHT="/.bash_history"    ## Path to your .bash_history File
if [ ${HOME}"${BHT}" -nt ${DBPATH}"${BHT}" ] 
then
    cat ${HOME}"${BHT}" > ${DBPATH}"${BHT}" 2>/dev/null;
 fi
## First make a file named after your files in the Dropbox folder
# That way no matter where you edit konsole bookmarks, If it's edited? 
# It's up-to-date on all Your devices.
    HLS=${HOME}"/.local/shere"
    KBM="/konsole/bookmarks.xml"
if [ ${HLS}"${KBM}" -nt ${DBPATH}"${KBM}" ] 
 then
        cat ${HLS}"${KBM}" > ${DBPATH}"${KBM}" 2>/dev/null;
    fi
fi
1
  • your solution is for Bash, not ZSH. ZSH has its own RC file
    – Thomas Ward
    Oct 26, 2021 at 12:33

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .