Difference between alias in zsh and alias in bash

The syntax of the alias command is the same in all Bourne-style shells. So you can share them across ~/.zshrc, ~/.bashrc, ~/.kshrc, ~/.shrc, as long as they make sense in all the shells.

The same holds for variable definitions and function definitions, as long as you use the subset of syntax that's supported in all shells.

If you don't use versions of zsh older than 4.0, you can put all your shell-agnostic definitions in a file called (say) ~/.common.rc.sh, where the first line is

emulate -LR sh 2>/dev/null

This tells zsh to expect sh compatible syntax in this file only. Then source that file near the beginning of ~/.bashrc, ~/.bashrc, ~/.kshrc and so on.

This is basically what I do. For example, I have somewhat complex code that generates an alias for ls with my favorite options depending on what's available (--color, -G, -F, etc.); it's shell-agnostic, so goes in .common.rc.sh. I also have shell-dependent aliases, like alias zcp='zmv -C' that goes in .zshrc.


According to the Zsh Workshop Aliases they seem to have the same syntax, so they should work.


zsh's alias allows global aliases, whereas bashs are only expanded at the beginning of the line.

In zsh:

alias -g L="| less -FRX"

You can then do:

verbose-command L

See here for a list of helpful global aliases.

Tags:

Bash

Alias

Zsh